Laravel Pluck() - Collection Method to Extract Values

Sovary July 25, 2022 590
2 minutes read

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.

What is Pluck() Method?

Pluck() - is used to extracts specific values from a collection. It is most useful when used with object arrays, but also work with arrays.

Examples

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.

Conclusion

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 Collections 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him