When searching for the first record, you want to perform some actions, when you don't find it. firstOrFail()
throws a 404 Exception.
You can use firstOr(function() {})
instead. Laravel got your covered
1$book = Book::whereCount('authors')2 ->orderBy('authors_count', 'DESC')3 ->having('modules_count', '>', 10)4 ->firstOr(function() {5 // THe Sky is the Limit ...6 7 // You can perform any action here8 });
Tip given by @bhaidar