Laravel Eloquent firstWhere() Example

By Hardik Savani | February 19, 2021 | 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 and laravel 8 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
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: