Laravel 5.2 - pluck() method returns array Laravel 5.2 - pluck() method returns array laravel laravel

Laravel 5.2 - pluck() method returns array


The current alternative for pluck() is value().


laravel pluck returns an array

if your query is:

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

then the array is like this (key is the index of the item. auto incremented value):

[    1 => "name1",    2 => "name2",    .    .    .    100 => "name100"]

but if you do like this:

$name = DB::table('users')->where('name', 'John')->pluck('name','id');

then the key is actual index in the database.

key||value[    1 => "name1",    2 => "name2",    .    .    .    100 => "name100"]

you can set any value as key.


In Laravel 5.1+, you can use the value() instead of pluck.

To get first occurence, You can either use

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

or use,

DB::table('users')->where('id', 1)->pluck('name')->first();