Laravel Eloquent/Collection pluck() method: 3 Practical Examples

In this short tutorial, we will see three examples of the Collections method pluck. I think it is rarely used and should be used more often.


Example 1: Dropdown Values

The most typical use case for using pluck() would be for showing dropdown values. For example, in the Controller, we have code which uses pluck() directly in the Eloquent query:

$roles = Role::pluck('name', 'id');

The second parameter tells pluck() to use the id column as a key. For example, if we have two roles Admin and User, the result would be:

> Role::pluck('title', 'id');
= Illuminate\Support\Collection {#5955
all: [
1 => "Admin",
2 => "User",
],
}

And then in the Blade, we would show all the roles as options of a dropdown select:

<select name="role_id">
@foreach($roles as $id => $role)
<option value="{{ $id }}">{{ $role }}</option>
@enforeach
</select>

Also, I have a YouTube video Eloquent Collection pluck: For Lists or For Dropdown Values where I showcase this example.


Example 2: Return Only Specific Field

The second example comes from the open-source project BookStackApp/BookStack.

class Popular extends EntityQuery
{
public function run(int $count, int $page, array $filterModels = null)
{
$query = $this->permissionService()
->restrictEntityRelationQuery(View::query(), 'views', 'viewable_id', 'viewable_type')
->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
->groupBy('viewable_id', 'viewable_type')
->orderBy('view_count', 'desc');
 
if ($filterModels) {
$query->whereIn('viewable_type', $this->entityProvider()->getMorphClasses($filterModels));
}
 
return $query->with('viewable')
->skip($count * ($page - 1))
->take($count)
->get()
->pluck('viewable')
->filter();
}
}

In this example, pluck() method is used after making the DB request, to return ONLY the relationship values.

Link to the source on GitHub: BookStackApp/BookStack


Example 3: Unique Values with Chains

In this example, we get unique values from the pluck. It isn't possible with pluck() directly, but pluck() is a Collection method, and Collection methods can be chained.

Let's say we have a lot of Products and want to get unique product names. So, in the Controller we would have:

$products = Product::pluck('name')->unique();

This code example will get all the products from the DB but will have only values from the name field.

And because this is now a Collection, we chain the unique to get only the unique values.


These are just a few code examples. You can find more open-source examples in the Code Example section of our website.

Also, if you want to learn more about Collection methods and how you can combine/chain them, I have a 40-minute course Laravel Collections Chains: 15 Real Examples.

avatar

Thanks for sharing this article, this saved me :)

avatar

So, is Product::pluck('name')->unique() more like DISTINCT in mysql?

avatar

You can say so, yes.

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials