Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect::route with parameter in URL in Laravel 5

I'm developing a Laravel 5 app, I have this route

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

In my controller, after I make a post call correctly, I want to redirect to that view, with this command:

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

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

Thank you.

like image 949
Bellots Avatar asked May 03 '15 21:05

Bellots


People also ask

How do I redirect a route in Laravel?

Laravel offers an easy way to redirect a user to a specific page. By using the redirect() helper you can easily create a redirect like the following: return redirect('/home'); This will redirect the user to the /home page of your app.

What are routes in Laravel explain Route handling with controller and route parameters?

You can define a route to this controller action, as: Route::get('user/{id}', 'UserController@show'); Route::resource: The Route::resource method can be a Restful Controller that produces all the basic routes required for an application and is dealt via controller class.


4 Answers

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

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

If it's only one you also don't need to write it as array:

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

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

return \Redirect::route('regions', ['id'=>$id,'OTHER_PARAM'=>'XXX',...])->with('message', 'State saved correctly!!!');
like image 101
lukasgeiter Avatar answered Oct 15 '22 07:10

lukasgeiter


You could still do it like this:

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

In cases where you have multiple parameters, you can pass the parameters as an array, for example: say you had to pass the capital of a particular region in you route, your route could look something like the following:

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

and you can then redirect using:

return redirect()->route('regions', ['id' => $id, 'capital' => $capital])->with('message', 'State saved correctly!');
like image 18
Awa Melvine Avatar answered Oct 15 '22 06:10

Awa Melvine


There are several ways to redirect this URL in laravel:

  1. Using URL with a global redirect helper function

    return redirect('states/'.$id.'/regions')->with('message', 'State saved correctly!!!');  
    
  2. Using named route

    return redirect()->route('regions', ['id' => $id])->with('message', 'State saved correctly!!!');
    
  3. Using controller action

    return redirect()->action('RegionController@index', ['id' => $id])->with('message', 'State saved correctly!!!');
    
like image 2
Satendra Maurya Avatar answered Oct 15 '22 07:10

Satendra Maurya


You can use the following

return redirect(route('addPost1',['pid',$post->id]));

OR

return redirect(route('addPost1'))->with('pid',$post->id);
like image 1
Siddhartha Avatar answered Oct 15 '22 07:10

Siddhartha