Laravel - Collections - Method Pluck

The pluck method retrieves all of the values for a given key:

    
    $collection = collect([
        ['product_id' => 'prod-100', 'name' => 'Desk'],
        ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]);
    
    $plucked = $collection->pluck('name');
    
    $plucked->all();
    
    // ['Desk', 'Chair']
	

You may also specify how you wish the resulting collection to be keyed:

    
    $plucked = $collection->pluck('name', 'product_id');
    
    $plucked->all();
    
    // ['prod-100' => 'Desk', 'prod-200' => 'Chair']
	

The pluck method also supports retrieving nested values using "dot" notation:

    
    $collection = collect([
        [
            'speakers' => [
                'first_day' => ['Rosa', 'Judith'],
                'second_day' => ['Angela', 'Kathleen'],
            ],
        ],
    ]);
    
    $plucked = $collection->pluck('speakers.first_day');
    
    $plucked->all();
    
    // ['Rosa', 'Judith']
	

If duplicate keys exist, the last matching element will be inserted into the plucked collection:

    
    $collection = collect([
        ['brand' => 'Tesla',  'color' => 'red'],
        ['brand' => 'Pagani', 'color' => 'white'],
        ['brand' => 'Tesla',  'color' => 'black'],
        ['brand' => 'Pagani', 'color' => 'orange'],
    ]);
    
    $plucked = $collection->pluck('color', 'brand');
    
    $plucked->all();
    
    // ['Tesla' => 'black', 'Pagani' => 'orange']