Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent - Get one Row

This might be a simple question, but I cannot figure this out. I am trying to get a user by email using:

$user = User::whereEmail($email)->get(); 

But this is returning an array (of dimension 1) of $users. So If I want to get the name, I have to do $user[0]['first_name'].

I tried using limit(1) or take(1), or even using ->toArray() but there was no difference.

What am I doing wrong?

like image 382
Kousha Avatar asked May 29 '14 03:05

Kousha


People also ask

How do you pluck in Laravel?

$users = User::all()->pluck('username'); You can also use pluck() method on nested objects like relationships with dot notation. Laravel Pluck method can be very useful when you extract certain column values without loading all the columns. You can benefit from the Laravel pluck method in the blade views as well.

Which method will retrieve the first result of the query in Laravel eloquent?

Using Laravel Eloquent you can get one row using first() method, it returns first row of table if where() condition is not found otherwise it gives the first matched row of given criteria.


1 Answers

Simply use this:

$user = User::whereEmail($email)->first(); 
like image 166
Ganesh Jogam Avatar answered Oct 01 '22 02:10

Ganesh Jogam