Skip to main content
Quick Tip

The findOrFail method also accepts a list of ids

The findOrFail method also accepts a list of ids. If any of these ids are not found, then it "fails".

Nice if you need to retrieve a specific set of models and don't want to have to check that the count you got was the count you expected

User::create(['id' => 1]);
User::create(['id' => 2]);
User::create(['id' => 3]);
 
// Retrieves the user...
$user = User::findOrFail(1);
 
// Throws a 404 because the user doesn't exist...
User::findOrFail(99);
 
// Retrieves all 3 users...
$users = User::findOrFail([1, 2, 3]);
 
// Throws because it is unable to find *all* of the users
User::findOrFail([1, 2, 3, 99]);

Tip given by @timacdonald87

Enjoyed This Tip?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.