Reputation: 12847
I have an array in my controller, and I want to redirect to another route with this array data.
class HomeController {
public function start() {
$dirs = ['X', 'Y'];
return redirect()->route('success', $dirs);
}
public function next($dirs) {
dd($dirs); // aim is getting data here
}
}
And my routes look like:
Route::post('/success', ['as' => 'success', 'uses' => 'HomeController@next']);
I tried get method but then I need to mess up the url and put a /success/{dirs}
there, which I don't want.
What is the proper way of achieving this?
Update:
Using this doesn't work :S
public function start() {
return redirect()->route('success', $dirs)->with('dirs', $dirs)->with('clientNames', $clientNames);
}
public function next() {
dd(session('dirs') // returns null
}
Upvotes: 2
Views: 1167
Reputation: 21681
You should try with this:
Append you data with()
method and access you data in another function with session:get()
method like:
use Illuminate\Http\Request;
class HomeController {
public function start(Request $request) {
$dirs = ['X', 'Y'];
return redirect()->route('success')->with('dirs', $dirs);
}
public function next(Request $request) {
$dirs = array();
if($request->session()->has('dirs')) {
$dirs = $request->session()->get('dirs');
}
}
}
Upvotes: 1
Reputation: 4915
The route
method's second argument is query parameters which will append your array values to the route like this /success?X&Y
.
function route($name, $parameters = [], $absolute = true)
{
return app('url')->route($name, $parameters, $absolute);
}
Instead of passing your array to the route, use with
method to put your data into the session and then retrieve it using session
helper function on the other route.
Documentaion: Redirecting with flashed session data
Try this
Route
Route::get('/success', 'HomeController@next')->name('success');
Controller
class HomeController
{
public function start()
{
$dirs = ['X', 'Y'];
return redirect()->route('success')->with('dirs', $dirs);
}
public function next()
{
$dirs = session('dirs');
}
}
Upvotes: 4