How to pass a variable as a route parameter when accessing the route in Laravel?

988 Views Asked by At

This does not work:

return redirect('/view-project-team/' . $projectRequest->id );

What is the right way to pass a variable into the route in this context?

1

There are 1 best solutions below

2
On

As was said in comments you should use name of the route:

return redirect()->route('view.project.team', ['id' => $projectRequest->id]);

Names can be defined in your router:

Route::get('/view-project-team/{id}', 'YourController@yourHandler')->name('view.project.team');

Note that:

  1. Dots in name of the route are not necessary (you could give any name).
  2. 'id' in route() call is refer to {id} in Route::get() call (names must match).