user3349495
user3349495

Reputation: 379

Laravel how to return single column value using Query Builder

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

Answers (11)

reza_qsr
reza_qsr

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

Hitesh
Hitesh

Reputation: 9

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

Upvotes: 0

NIDHINgL
NIDHINgL

Reputation: 172

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

Upvotes: -2

S Debasish Nayak
S Debasish Nayak

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

Lonac Lonac
Lonac Lonac

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

17K
17K

Reputation: 471

According to the latest documentation: 5.6

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

will do the trick.

Upvotes: 4

Vinil Vinayan
Vinil Vinayan

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

Ankit Vishwakarma
Ankit Vishwakarma

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

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

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81147

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"

Upvotes: 41

itachi
itachi

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

Related Questions