In this article, we will explain about Laravel 9 pluck()
method with example. You will quick understand how to use pluck method in Laravel with this article example.
Pluck() - is used to extracts specific values from a collection. It is most useful when used with object arrays, but also work with arrays.
Example #1 - Extract Values by Keys
<?php
$collection = collect([
['id' => '001', 'name' => 'Apple', 'email' => 'mail1@ex.com'],
['id' => '002', 'name' => 'Banana', 'email' => 'mail2@ex.com' ]
]);
$plucked = $collection->pluck('email');
dd($plucked->all());
//Example extract from User Model
$emails = User::pluck('email');
Output #1
array:2 [▼
0 => "mail1@ex.com"
1 => "mail2@ex.com"
]
Suppose you want to get value email from User Model to extract single column from collection. What if I put 2 arguments in pluck()
method? look at example #2
Example #2 - Convert Value to Associative Array
<?php
$collection = collect([
['id' => '001', 'name' => 'Apple', 'email' => 'mail1@ex.com'],
['id' => '002', 'name' => 'Banana', 'email' => 'mail2@ex.com' ]
]);
$plucked = $collection->pluck('email','id');
dd($plucked->all());
// ['001' => 'mail1@ex.com', '002' => 'mail2@ex.com']
//Example extract from User Model
$emails = User::pluck('email','id');
Output #2
array:2 [▼
"001" => "mail1@ex.com"
"002" => "mail2@ex.com"
]
The method accept 2 arguments helps you to manipulated multiple columns in key-value pair by convert values to associated array second parameter as key, first as value.
Example #3 - Extract Nested Values or Relationship Model
<?php
$collection = collect([
[
'id' => '1',
'name' => [
'keywords' => ['Laravel', 'PHP'],
],
],
[
'id' => '2',
'name' => [
'keywords' => ['SEO','Boost SEO','Trend'],
],
],
]);
$plucked = $collection->pluck('name.keywords');
dd($plucked->all());
// [['Laravel', 'PHP'], ['SEO','Boost SEO','Trend']]
//Example extract from User Model relationship with SEO
$users = Users::with('seo')->get();
$keywordAll = $users->pluck('seo.keywords');
Output #3
array:2 [▼
0 => array:2 [▼
0 => "Laravel"
1 => "PHP"
]
1 => array:3 [▼
0 => "SEO"
1 => "Boost SEO"
2 => "Trend"
]
]
You can use pluck()
method on nested objects like relationships with dot notation.
Example #4 - Remove duplicate key
<?php
$collection = collect([
['name' => 'A', 'gender' => 'male'],
['name' => 'A', 'gender' => 'female'],
['name' => 'B', 'gender' => 'male'],
['name' => 'B', 'gender' => 'female'],
]);
$plucked = $collection->pluck('gender', 'name');
dd($plucked->all());
Output #4
array:2 [▼
"A" => "female"
"B" => "female"
]
Likely example #1 but if duplicate keys exist, the last matching element will be inserted.
Laravel Pluck method using when you need to extract specific column values without loading all of the columns. That's all for now; if you have any questions, leave them in the comments section below.
You might also like:
Laravel PHP Laravel 9 Laravel CollectionsFounder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him