Laravel 5.2 - pluck() method returns array

242,416

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

I use laravel 7.x and I used this as a workaround:->get()->pluck('id')->toArray();

it gives back an array of ids [50,2,3] and this is the whole query I used:

   $article_tags = DB::table('tags')
    ->join('taggables', function ($join) use ($id) {
        $join->on('tags.id', '=', 'taggables.tag_id');
        $join->where([
            ['taggable_id', '=', $id],
            ['taggable_type','=','article']
        ]);
    })->select('tags.id')->get()->pluck('id')->toArray();
Share:
242,416
Limon Monte
Author by

Limon Monte

Has SweetAlert2 helped you create an amazing application? You can show your support by making a donation: https://github.com/sponsors/limonte https://www.paypal.me/limonte

Updated on July 05, 2022

Comments

  • Limon Monte
    Limon Monte almost 2 years

    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 to pluck. 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) }
    
  • Limon Monte
    Limon Monte over 8 years
    Thank you! This absolutely should be in the upgrade guide as it's breaking change.
  • Limon Monte
    Limon Monte over 8 years
    I see that it is in upgrade guide for 5.1: laravel.com/docs/5.2/upgrade#upgrade-5.1.0 My bad I didn't catch this change in prev release.
  • Limon Monte
    Limon Monte over 8 years
    There is actually notice about this change: "The Eloquent collection instance now returns a base Collection (Illuminate\Support\Collection) for the following methods: pluck, ..."
  • Harry Bosh
    Harry Bosh over 7 years
    need to add ->get(); you still need to use PHP to format the array
  • Ankit Balyan
    Ankit Balyan over 7 years
    Php operations are much faster than Mysql queries.
  • Felix Eve
    Felix Eve almost 7 years
    @AnkitBalyan are you being sarcastic? mysql is MUCH faster than php. Try processing a million rows in each...
  • DragonFire
    DragonFire over 4 years
    It is much easier to use value as it is in ready non array form.. especially in toggle type operations
  • CodeConnoisseur
    CodeConnoisseur almost 3 years
    @user1669496 THANK YOU SIR
  • CodeToLife
    CodeToLife almost 3 years
    AModel::select('id',...)->where(...)->get()->pluck('id'); gives array of integer values of id, when AModel::select('id',...)->where(...)->get()->pluck('id')->to‌​Array(); gives array alike [0=>id0, 1=>id1,...., n=>idN], Have tested.
  • ddruganov
    ddruganov over 2 years
    can someone please explain the reasoning behind such naming? why not "column"?