Controllers in general with laravel

Julian Mendez Source

Imagine I have this route

Route::get('/services/{service}', '[email protected]');

when I do

public function show($s)
{
  $service = Services::findOrFail($s)
}

where $s is a string that might be something like "clean" that has the id 1 for example.

It's better to make the route I have or find it for the ID like

Route::get('/services/{id}', '[email protected]');

public function show($id)
{
  $service = Services::findOrFail($id)
}

It doesn't matter the function I use, the important thing here if it is valid to search by name rather than id

phpcontrollerslaravel-5.6

Answers

answered 2 months ago Davit #1

when $s is sting you must be make sure for your databse table correspond column is unique. For avoid that you can make your route

Route::get('/services/{id}/{service}', '[email protected]');

public function show($id, $service)
{
  $service = Services::where(['id' => $id, 'service' => $service])->first()
}

comments powered by Disqus