Code Worm
Code Worm

Reputation: 323

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

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?

Upvotes: 1

Views: 1175

Answers (1)

Petr
Petr

Reputation: 460

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).

Upvotes: 5

Related Questions