Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, how to pass object through redirect() route()?

Tags:

php

laravel

My goal is to pass object from current function to other function. As you can see the codes below, I have provided a multiple return approach I've done in the controller section. I have commented each of its error. I am not sure why that is happened although some of the approaches are from the voted answer in some other questions.

Array Content [ dump($data = $request->all()); ]

enter image description here

Controller

public function supply_status(Request $request, $data)
{
    dump($data);
}

public function supply_redirect(Request $request)
{
    $data = $request->all();

    //option 1
    return redirect()->route('supply_status', compact('data')); // "Array to string conversion" error

    //option 2
    return redirect()->route('supply_status', ['data' => $data]); // "Array to string conversion" error

    //option 3
    return redirect()->route('supply_status')->with('data', $data); // Missing required parameters for [Route: supply_status] [URI: supply_status/{data}].

    //option 4
    return redirect()->action('TestController@supply_status')->with('data', $data); // Missing required parameters for [Route: supply_status] [URI: supply_status/{data}].
}

Route

Route::get('supply_status/{data}', 'TestController@supply_status')->name('supply_status');
like image 598
LearnProgramming Avatar asked Nov 27 '22 01:11

LearnProgramming


1 Answers

For this situation you really should use the Session class

Session::put('customer_data', $request->all());
Session::save();
redirect()->route('foobar');

in foobar

if($data = Session::get('customer_data')) {
   dump($data);
}

You could alternatively also do: redirect()->with(['customer_data' => $request->all()]) and then fetch it the same way with Session::get('customer_data') or session()->get('customer_data')

like image 103
Tschallacka Avatar answered Nov 29 '22 05:11

Tschallacka