Laravel 9 Pluck Method Example

$collection = collect([
['id' => '1', 'name' => 'Laravel'],
['id' => '2', 'name' => 'PHP'],
]);

$plucked = $collection->pluck('name');

$plucked->all();

// ['Laravel', 'PHP']
$plucked = $collection->pluck('name', 'id');

$plucked->all();

// ['1' => 'Laravel', '2' => 'PHP']
$collection = collect([
[
'id' => '1',
'name' => [
'car' => ['Audi Q5', 'Audi A8'],
],
],
[
'id' => '2',
'name' => [
'car' => ['Mercedes-Benz C-Class', 'Mercedes-Benz S-Class'],
],
],
]);

$plucked = $collection->pluck('name.car');

$plucked->all();

// [['Audi Q5', 'Audi A8'], ['Mercedes-Benz C-Class', 'Mercedes-Benz S-Class']]
$collection = collect([
['brand' => 'Tesla', 'color' => 'red'],
['brand' => 'Toyota', 'color' => 'white'],
['brand' => 'Tesla', 'color' => 'black'],
['brand' => 'Toyota', 'color' => 'grey'],
]);

$plucked = $collection->pluck('color', 'brand');

$plucked->all();

// ['Tesla' => 'black', 'Toyota' => 'grey']
public function index(){

$names = Users::pluck('name', 'id');

dd($names);
}

// ['1' => 'websolutionstuff', '2' => 'websolution']

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

96 Followers

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.