简体   繁体   中英

How to return redirect with compact variable in laravel

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?

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');;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM