Laravel Throw 404 if entity not found


Example

If you want to automatically throw an exception when searching for a record that isn't found on a modal, you can use either

Vehicle::findOrFail(1);

or

Vehicle::where('make', 'ford')->firstOrFail();

If a record with the primary key of 1 is not found, a ModelNotFoundException is thrown. Which is essentially the same as writing (view source):

$vehicle = Vehicle::find($id);

if (!$vehicle) {
    abort(404);
}