Laravel 8 firstOrFail Eloquent Query Example

Hello artisan,

There is a new helper method called firstOrFail is released in Laravel 8.x version. This method will help us to fetch data from the database very efficiently to handle errors. In this firstOrFail eloquent query example, I will show you how you can create a query to get a single row or a table in Laravel.

Before releasing this firstOrFail method, we can use first() helper, but now we can use this firstOrFail method to fetch the first row of a table more efficiently. So I will show you that query how we can use firstOrFail method in Laravel eloquent.

You can use this helper to handle errors easily. If the query does not find any data, then it will automatically return the 404 page in Laravel. Let's see the example query of firstOrFail() in Laravel:

$collection = new Collection([
    ['name' => 'foo'],
    ['name' => 'bar'],
try {
   $collection->where('name', 'fish')->firstOrFail()

catch (ItemNotFoundException){


Hope it can help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js