Laravel Eloquent - Get one Row

Kousha picture Kousha · May 29, 2014 · Viewed 174.1k times · Source

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?

Answer

Ganesh Jogam picture Ganesh Jogam · May 29, 2014

Simply use this:

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