If I want to get the name of the first user in the database using eloquence, I can do something like this:
$user = User::select('name')->first()->pluck('name');
// or $user = User::first()->pluck('name');
echo $user;
to get only the name of this user as a string.
However, if I try to use the same query building method:
$user = DB::table('users')->select('name')->first()->pluck('name');
echo $user;
I get an exception:
Call the undefined method stdClass :: pluck ()
But without using it, it will work first:
$user = DB::table('users')->select('name')->where('id',1)->pluck('name');
echo $user;
Can't I use pluckwith firstusing the query builder or am I something wrong?
PS. Of course, I know that I can display any property with the help of $user->nameno use pluck, but I'm just wondering why using Eloquent works, and with the help of Query Builder it works only when it does not like first, andpluck