laravel – Pass parameter from one route to another


I need to get a variable from the url and redirect with the parameter to another function

Route::get('doacao/{id}', function(Request $request){
   return Redirect::route('escolha/doacao')->with('id',$request->id);

Redirecting to this other route to get the parameter in the @retornaAssociado function:

Route::get('escolha/doacao', ['as' => 'escolha/doacao', 'uses' => 'Site\CadastroController@retornaAssociado']);

I tried several ways to get the id inside the function, but I couldn't. I need two routes because I don't want the id to appear in the url when I return to the view.

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

   return view('layouts/pagamento')->with('id', $data['id']);  



Wouldn't it be correct to pass a $id variable to capture the route parameter?

Route::get('doacao/{id}', function(Request $request, $id){
   return Redirect::route('escolha/doacao')->with('id',$id);

Then in the following route, to capture this parameter, you need to get the value that is in session .

public function retornaAssociado(Request $request){

   return view('layouts/pagamento')->with('id', session('id'));  


When you use the with method of Redirect , you are telling laravel to store the data in a session flash. I believe that this is not the viable case, because if you refresh the pages, this data will disappear. This is because the flash, after being used, is removed from the session

I think it would be better for you to add an optional parameter in the second route, to receive this parameter from another url, but if you don't receive it, the page is also displayed normally.


Route::get('escolha/doacao/{id?}', ['as' => 'escolha/doacao', 'uses' => 'Site\CadastroController@retornaAssociado']);

public function retornaAssociado(Request $request, $id = null){

   return view('layouts/pagamento')->with('id', $id);  


If someone accesses escolha/doacao/1 , the value of $id will be 1 . If you access escolha/doacao , the value will be NULL .

But also note that it is necessary, in the act of redirection, to pass the $id as a parameter, so that it is redirected to this route with the same parameter:

Route::get('doacao/{id}', function(Request $request, $id){
    return Redirect::route('escolha/doacao', $id);

The code above will result in a redirect to "escolha/doacao/{$id}" . So, if someone accesses doacao/5 , they will be redirected to escolha/doacao/5 . But it does not prevent the person from accessing escolha/doacao directly.


If the questioner's intention is to redirect to another url, hiding the value of $id passed in doacao/$id , I suggest using session (I'm not talking about with , because the value is temporary).

You could do:

Route::get('doacao/{id}', function(Request $request, $id){

   session('', $id);

   return Redirect::route('escolha/doacao');

To retrieve this value, just call session('') .

Scroll to Top