37

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;

2
  • 1
    I think it's because you're not retrieving a single result. so, if you want the user_id, you can iterate over $myquery, or select the first result )->first() Commented Nov 14, 2016 at 13:58
  • As @GiuServ said : get() will return an array of attendance objects even if there is only one object matched the where conditions. Where first() will return the first matched object from the result. Even more than one object is matched where conditions. Commented Nov 3, 2017 at 6:25

11 Answers 11

41

EDIT: As of version 5.1 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"
Sign up to request clarification or add additional context in comments.

4 Comments

Where did you read that pluck is deprecated in 5.1? Is value() the alternative?
@GiuServ Use the source, Luke! github.com/laravel/framework/blob/5.1/src/Illuminate/Database/… And yes, value is the new pluck
Nevermind, i just understand that pluck function as changed from 5.1. I started directly from 5.3 and didn't get that previously it didn't had the same behaviour!
laravel.com/docs/5.6/queries Retrieving A Single Row / Column From A Table $email = DB::table('users')->where('name', 'John')->value('email'); Retrieving A List Of Column Values $titles = DB::table('roles')->pluck('title');
28

That's for Laravel 5.3:

$user_id = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->value('user_id');

3 Comments

select('user_id') isn't needed
It works but if no rows found you'll get an exception "Call value() on null"
@fdehanne - that's not entirely accurate. Although you might think it's redundant, getting into a good habit of ONLY selecting what you need from the database will make your application more performant, as less data is required for your query responses. As your grab grows, that data transfer gets quite large, and takes longer to transfer and process to your app servers. So it's a very good idea to only select what you need.
10

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

Comments

9

This will return user_id only

$myquery = DB::table('attendances')->where('date_only', $newdate)->select('user_id')->pluck('user_id')->first();

Comments

4

According to the latest documentation: 5.6

DB::table('users')->select('name');

will do the trick.

Comments

1

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();

Comments

0

DB::table('yourtablename')->where('id', '=', $id)->orderBy('id','asc')->pluck('user_id')->toArray();

3 Comments

This simply repeats code from other answers. Please don't do that.
@Chris Actually this answer different, The point is toArray method.
@Parsa, that's not meaningfully different in the context of the question being asked.
0

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

Comments

-2

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>

Comments

-2

DB::table('table_name')->select('column_name');

1 Comment

Should explain your answer
-3
 $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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.