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?
$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.
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.
Simply use this:
$user = User::whereEmail($email)->first();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With