Laravel Eloquent firstOr() Example

By Hardik Savani | February 19, 2021 | Category : Laravel

This tutorial will provide example of laravel firstOr. i would like to show you laravel eloquent firstor example. if you want to see example of laravel firstor example then you are a right place. we will help you to give example of firstor in laravel.

Here i will give you very simple example of how to use firstOr() with laravel eloquent. you can easily use firstOr() with laravel 6, laravel 7 and laravel 8 version.

Sometime, you need to write logic for default value. when you find some product from database and it's not match any record then you can return default product. so you have to write long logic behind this but laravel eloquent provide firstOr() where you can easily return default object.

Let's see example:

firstOr() Example:


namespace App\Http\Controllers;

use App\Models\Category;

class SignaturePadController extends Controller



* Write code on Method


* @return response()


public function index()



We can ignore to write this query.

$category = Category::where("name", "Mobile2")->first();


$category = Category::where("name", "Laptop")->first();



$category = Category::where("name", "Mobile2")->firstOr(function () {

return Category::where("name", "Laptop")->first();





I hope it can help you...

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: