I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me:
The
lists
method on the Collection, query builder and Eloquent query builder objects has been renamed topluck
. The method signature remains the same.
That's ok, rename refactoting from lists()
to pluck()
isn't a problem. But what with useful pluck()
method which was in L5.0 and L5.1?
From the 5.0 documentation:
Retrieving A Single Column From A Row
$name = DB::table('users')->where('name', 'John')->pluck('name');
What is the alternative for old pluck()
method in L5.2?
UPDATE:
Example:
var_dump(DB::table('users')->where('id', 1)->pluck('id'));
L5.1:
// int(1)
L5.2:
// array(1) { [0]=> int(1) }
In the original example, why not use the select() method in your database query?
This will be faster than using a PHP framework, for it'll utilize the SQL query to do the row selection for you. For ordinary collections, I don't believe this applies, but since you're using a database...
Larvel 5.3: Specifying a Select Clause
The current alternative for
pluck()
isvalue()
.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)
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.
you can set any value as key.
To get first occurence, You can either use
or use,