How to return redirect with compact variable in laravel

1k views Asked by At

I have code to do a return redirect as follows

public function addWarrent(Request $request)
      {
        $add_warrent = new WarrentModel();
        $add_warrent->id_warrent          = $request->input('id_warrent');
        $add_warrent->workunit_id         = Auth::user()->workunit_id;
        $add_warrent->warr_name           = $request->input('warr_name');
        $add_warrent->warr_position       = $request->input('warr_position');
        $add_warrent->warr_category       = $request->input('warr_category');
        $add_warrent->warr_status         = "Belum Diproses";
        $add_warrent->warr_dt             = Carbon::now();
        $add_warrent->warr_tm             = Carbon::now();
        $add_warrent->save();
    
        Excel::import(new WarrentItemImport, request()->file('warrent_item'));
        
        $warrent_entry_id   = WarrentItemModel::where('warrent_entry_id', null)
                                ->update([
                                  'warrent_entry_id'  => $request->id_warrent,
                                ]);
    
        $add_head_workunit  = WorkunitModel::where('id_workunit', Auth::user()->workunit_id)
                                ->update([
                                  'workunit_head_nip'  => $request->workunit_head_nip,
                                  'workunit_head_name' => $request->workunit_head_name
                                ]);
    
        $id = $request->input('id_warrent');
        return redirect('satker/print_warrent', compact('id'))->with('success','Berhasil Membuat Surat');
      }

And then, I got an error message

Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, array given, called in D:\XAMPP\htdocs\Warehouses\vendor\laravel\framework\src\Illuminate\Routing\Redirector.php on line 233

Why did it happen?

1

There are 1 answers

0
Ali Raza On

Try one of following

 return redirect('/satker/print_warrent/' . $id)->with('success', 'Berhasil Membuat Surat.');

or

 return redirect()->route('/satker/print_warrent/')
  ->with(compact('id'))
  ->with('success','Berhasil Membuat Surat');;

or

 return redirect()->route('/satker/print_warrent/')
   ->with('id', $id)
   ->with('success','Berhasil Membuat Surat');;