phpcontrollerslaravel-5.6

Controllers in general with laravel


Imagine I have this route

Route::get('/services/{service}', 'ServiceController@show');

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}', 'ServiceController@show');

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


Solution

  • 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}', 'ServiceController@show');
    
    public function show($id, $service)
    {
      $service = Services::where(['id' => $id, 'service' => $service])->first()
    }