Laravel Elouqent firstWhere() Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this example,I will show you how we can use firstwhere in laravel application . we can simply and also easy to use the firstwhere in laravel application .

The firstWhere method is returns the first record for the table from the database. So here i will use email like abc@gmail.com and where equal get records from the database as bellow example.

Syntax :

firstWhere('colume name','value');

Example :

So if we want to use firstWhere() then we can ignore one where condition. Now here we can see on the bellow where condition and first record and also we can see by using firstWhere().

$user = User::where('email', 'kevalkashiyani9@gmail.com')->first();

dd($user);

Here I am sure that the firstWhere will help us more. But If we have laravel 6 then and then we can use.

Let’s see bellow example:

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $users = User::firstWhere('email', 'kevalkashiyani9@gmail.com');
                
    dd($users);
}

Output :

array:9 [?
    "id" => 1
    "name" => "keval"
    "email" => "kevalkashiyani9@gmail.com"
    "email_verified_at" => null
    "password" => "123456"
    "active" => 0
    "remember_token" => null
    "created_at" => null
]

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →