Laravel Eloquent firstWhere() Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent withSum() and withCount() Example
- Laravel Eloquent Group By Example
- Delete All Records from Table in Laravel Eloquent
- Laravel Eloquent inRandomOrder() Method Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Multiple orWhere Condition in Laravel Eloquent
- Laravel Eloquent Where Query Examples
- How to Create and Use Query Scope in Laravel Eloquent
- Laravel Has Many Through Eloquent Relationship Tutorial