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

1User::create(['id' => 1]);
2User::create(['id' => 2);
3User::create(['id' => 3]);
4 
5// Retrives the user...
6$user = User::findOrFail(1);
7 
8// Throws a 404 because the user doesn't exist...
9User::findOrFail(99);
10 
11// Retrives all 3 users...
12$users = User::findOrFail([1, 2, 3]);
13 
14// Throws because it is unable to find *all* of the users
15User::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:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord