1

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?

Code Worm
  • 313
  • 1
  • 4
  • 16
  • and what's the error that you get? – nakov Apr 15 '19 at 19:35
  • you should name the route, then use `return redirect()->route('nameOfRoute', ['id' => 1]);` – Derek Pollard Apr 15 '19 at 19:35
  • As @DerekPollard said, you can generate url to named route. [Named Routes docs](https://laravel.com/docs/5.8/routing#named-routes) – ljubadr Apr 15 '19 at 19:37
  • and how and where do you have name a route? – Code Worm Apr 15 '19 at 19:37
  • I did this `Route::get('/view-project-team/{project_request_id}', 'SinglePageController@viewProjectTeam')->name('viewProjectTeam');`and this `return redirect()->route('viewTeamProject', ['project_request_id' => $projectRequest->id ]);` but now I get a 500 error – Code Worm Apr 15 '19 at 19:53

1 Answers1

5

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).
Petr
  • 420
  • 3
  • 13
  • when I implement this I get a 200 status code on redirect. Why does that happen? – Code Worm Apr 16 '19 at 06:02
  • I don't know why is that, but calling `route()` method that way always return 200. If you need to specify another code, you can pass it as a third argument to `route()` call. Like `... ['id' => $projectRequest->id], 302);`. – Petr Apr 16 '19 at 11:00