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

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 73 courses
  • 93 long-form tutorials
  • access to project repositories
  • access to private Discord

Recent New Courses