lundi 4 juillet 2016

Laravel: checking id in controller to be certain instance

I have a Laravel application with the following route:

Route::get('event/{id}/stages', [
            'as' => 'events_list',
            'uses' => 'EventsController@show'
]);

In the controller I define the method show as follows:

 public function show($id) {
        $event = Event::find($id);
        return view('showEvent', ['event' => $event]);
  }

The issue is that I can pass whatever value in {id}, so it should work for

but it should not work for

The first one should not work because 20 is not a valid id (there are only 4 events) and foo should not work because it's a string. Both should be checked and redirected to a 404 page.

Ideally I can just check if the {id} is an Event instance that has an entry in the database.

What is the best way to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire