Reputation: 379
I want to use the data from the SQL Query, to explain it further here is my code:
$myquery = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
$myquery->user_id;
Now I want the value of my $myquery to be USER_ID's value, I get error if I used the Above code -$myquery->user_id;
Upvotes: 35
Views: 126792
Reputation: 61
This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
base on this link from laravel docs : https://laravel.com/docs/11.x/queries#retrieving-a-single-row-column-from-a-table
Upvotes: 0
Reputation: 9
DB::table('yourtablename')->where('id', '=', $id)->orderBy('id','asc')->pluck('user_id')->toArray();
Upvotes: 0
Reputation: 263
To get only the list of value of a single column you can do like this.
$result = CustomerInfo::select('eventDate')->pluck('eventDate');
here $result will give you the result like this:
["2019-03-25","2020-03-31","2019-03-31","2019-04-24","2019-11-26"]
hope it may help someone
Upvotes: 10
Reputation: 1
If you have data in array already and you want to filter only such kind of data, you can do this as well
0 => "first_name"
1 => "middle_name"
]
$users_data = User::where('id', '1')->get($filter_data );
return $users_data; ```
<pre> In laravel 5.4 works perfect for me </pre>
Upvotes: -2
Reputation: 471
According to the latest documentation: 5.6
DB::table('users')->select('name');
will do the trick.
Upvotes: 4
Reputation: 109
This will return user_id only
$myquery = DB::table('attendances')->where('date_only', $newdate)->select('user_id')->pluck('user_id')->first();
Upvotes: 9
Reputation: 1691
I think there is some confusion; but I am using Laravel 5.3 and pluck and value function is not deprecated as depicted above.
For more clarification: pluck() will be used instead of get() when you want all the values only value() will be used instead of first() when you want only one value.
use method all() to get the array of itmes. usage:
\App\User::whereIn('mobile',['971700000', '965000000'])->select('id')->pluck('id')->all();
Upvotes: 1
Reputation: 279
That's for Laravel 5.3:
$user_id = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->value('user_id');
Upvotes: 26
Reputation: 81147
pluck
is deprecated, since 5.2 its meaning is completely different (as lists
used to be), so use value
method instead.You're trying to get property of the array, so obviously you're getting error.
If you need single field only, then use pluck
(which returns single value - string by default):
// let's assume user_id is 5
DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->pluck('user_id'); // "5"
When you need whole row, then first
(which returns stdObject):
$att = DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->first();
$att->user_id; // "5"
$att->any_other_column;
And get
is when you want multiple results (which returns simple array of stdObjects):
$result = DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->get();
$result; // array(0 => stdObject {..}, 1 => stdObject {..}, ...)
$result[0]->user_id; // "5"
Upvotes: 41
Reputation: 6393
$myquery = DB::table('attendances')->select('user_id')
->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
by default, the returned data will be array of objects. you need to loop over it.
e.g.
foreach($myquery as $i)
{
echo $i->user_id;
}
Upvotes: -3