Thameera's avatar

get retrieved value of database as single input

$user = DB::table('users')->where('name', 'John')->pluck("phonenumber"); when i use this type query it gives as array datatype even it is a single value how to get it as single value

0 likes
4 replies
topvillas's avatar

$user = DB::table('users')->where('name', 'John')->first()->pluck("phonenumber");

1 like
Thameera's avatar

This code gives error as Call to undefined method stdClass::pluck()

gregrobson's avatar

What version of Laravel are you using? In Laravel 5.1 pluck() was renamed to value().

// 5.0 and earlier
$userPhoneNumber = DB::table('users')->where('name', 'John')->pluck('phonenumber');

// 5.1 onwards
$userPhoneNumber = DB::table('users')->where('name', 'John')->value('phonenumber');

https://laravel.com/docs/5.1/upgrade

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

if you use pluck as part of a query then you will get an array of results

using first will return the first of possibly many matches and the you can access the attributes directly without using pluck

$user = DB::table('users')->where('name', 'John')->first()->phonenumber;

Please or to participate in this conversation.