Source: sebastiandedeyne.com

Eloquent findOrFail caveats

Category: Laravel

I use Model::findOrFail a lot in Laravel. Recently, I realized it’s not always the best option.

$title = Post::findOrFail($id)->title; Another benefit is that Laravel returns a 404 response for you when the model doesn’t exist. It’s a great feature in controllers and other outer layers of your application, but can be harmful in core business logic.

In addition, Laravel doesn’t report model not found exceptions to error trackers.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners