Redirect :: route with parameter in URL in Laravel 5

I am developing a Laravel 5 application, I have this route

Route::get('states/{id}/regions', ['as' => 'regions', 'uses' => ' RegionController@index ']);

In my controller, after I have correctly completed the mail call, I want to redirect to this view using this command:

 return \Redirect::route('regions')->with('message', 'State saved correctly!!!'); 

The problem is that I don’t know how to pass the {id} parameter, which should be in my URL.

Thanks.

+5
source share
4 answers

You can pass route parameters as the second argument to route() :

 return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!'); 

If this is only one, you also do not need to write it as an array:

 return \Redirect::route('regions', $id)->with('message', 'State saved correctly!!!'); 

If your route has more parameters or it has only one, but you want to clearly indicate which parameter each value has (for readability), you can always do this:

 return \Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!'); 
+7
source

You can still do it like this:

 return redirect()->route('regions', $id)->with('message', 'State saved correctly!!!'); 

In cases where you have several parameters, you can pass the parameters as an array, for example, say that you need to pass the capital of a certain region in your route, your route may look something like this:

 Route::get('states/{id}/regions/{capital}', ['as' => 'regions', 'uses' => ' RegionController@index ']); 

and you can redirect with:

 return redirect()->route('regions', ['id' = $id, 'capital' => $capital])->with('message', 'State saved correctly!!!'); 
0
source

You can pass the parameter {id} with such a redirect

 return \Redirect::route('regions', [$id])->with('message', 'State saved correctly!!!'); 
0
source

There are several ways to redirect this url to laravel:
1. Using url with global helper redirection function
return redirect('states/'.$id.'/regions')->with('message', 'State saved correctly!!!');
2. Using a named route
return redirect()->route('regions', ['id' => $id])->with('message', 'State saved correctly!!!');
3. Using a controller action
return redirect()->action(' RegionController@index ', ['id' => $id])->with('message', 'State saved correctly!!!');

0
source

All Articles