Laravel Eloquent firstWhere() Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi,

This post will give you example of laravel firstWhere. i explained simply about laravel eloquent firstwhere example. you'll learn laravel firstwhere example. We will use firstwhere in laravel.

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

When we need to get record from id then we can easily get from find() method, but when you need to get record from slug or name then you have to use first() method. But laravel eloquent provide firstWhere() method that will help you to easily get match first record.

Let's see example:

firstWhere() Example:

<?php

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", "Mobile")->first();

*/

$category = Category::firstWhere("name", "Mobile");

dd($category);

}

}

I hope it can help you...

Shares