Laravel Pluck

             Laravel pluck method is used to retrieve the list of values from the collection. It has two parameters $value and $key. Pluck is used when you have some data and you need only specific parts of the data. Laravel pluck is a methods a phase under the Collection class and it is used for working with arrays of data. In this the $value is used to represent which property should become the value in the resulting collection. The $key is used to indicate which property should become the key in the result. Look at the below example to learn how to use pluck.

// First we collect the participants

$participants = collect([

    ['name' =>'John', 'age' => 55],

    ['name' =>'Melissa', 'age' => 18],

    ['name' =>'Bob', 'age' => 43],

    ['name' => 'Sara', 'age' => 18],

]);

 

// Then we ask the collection to fetch all the names

$namesList = $partcipants->pluck('name')

// ['John', 'Melissa', 'Bob', 'Sara'];