ItSolutionStuff.com

Laravel Eloquent firstWhere() Example

By Hardik Savani • April 16, 2024
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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent withSum() and withCount() Example

Read Now →

Laravel Eloquent Group By Example

Read Now →

Delete All Records from Table in Laravel Eloquent

Read Now →

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Eloquent whereNotBetween() Query Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Multiple orWhere Condition in Laravel Eloquent

Read Now →

Laravel Eloquent Where Query Examples

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →