Laravel's Pluck Gets Smarter: Closure Support for Custom Transformations

Laravel's pluck()
method just got more powerful with closure support for both key and value parameters. No more switching to mapWithKeys()
when you need simple formatting transformations.
From Verbose mapWithKeys to Clean pluck
Previously, when you needed to apply formatting or transformations while plucking values, you had to abandon the simple pluck()
method and use the more verbose mapWithKeys()
approach:
// The old way - verbose mapWithKeys for simple transformations
$countries = Country::get()->mapWithKeys(function ($country) {
return [$country->id => "{$country->flag} {$country->name}"];
});
Now, with closure support, you can achieve the same result with a cleaner pluck()
call:
// The new way - closures in pluck parameters
$countries = Country::get()
->pluck(fn (Country $country) => "{$country->flag} {$country->name}", 'id');
Real-World Example
Consider a simple user management system where you need to create formatted dropdown options:
<?php
namespace App\Controllers;
use App\Models\User;
use App\Models\Product;
class DropdownController extends Controller
{
public function getUsers()
{
// Format full names for dropdown
$users = User::get()
->pluck(fn ($user) => "{$user->first_name} {$user->last_name}", 'id');
return response()->json($users);
// Output: {1: "John Doe", 2: "Jane Smith"}
}
public function getProducts()
{
// Add currency symbol to prices
$products = Product::get()
->pluck(fn ($product) => "$" . number_format($product->price, 2), 'name');
return response()->json($products);
// Output: {"Laptop": "$999.99", "Mouse": "$29.99"}
}
public function getCategories()
{
// Create slugs as keys
$categories = Category::get()
->pluck('name', fn ($category) => strtolower(str_replace(' ', '-', $category->name)));
return response()->json($categories);
// Output: {"electronics": "Electronics", "home-garden": "Home & Garden"}
}
}
In Blade templates, this makes dropdowns much cleaner:
{{-- Before: needed separate variable or complex logic --}}
<select name="user_id">
@foreach($users as $id => $fullName)
<option value="{{ $id }}">{{ $fullName }}</option>
@endforeach
</select>
{{-- After: directly use formatted pluck results --}}
<select name="product">
@foreach($products as $name => $price)
<option value="{{ $name }}">{{ $name }} - {{ $price }}</option>
@endforeach
</select>
The closure support makes these simple transformations much cleaner. Instead of using mapWithKeys()
for basic formatting, you can now handle common cases like:
- Concatenating fields: First name + last name
- Adding prefixes/suffixes: Currency symbols, units
- Simple transformations: Lowercase, formatting
- Creating slugs: For URLs or CSS classes
This keeps your code concise while maintaining the familiar pluck()
syntax you already know.
Stay Updated with More Laravel Tips
Enjoyed this article? There's plenty more where that came from! Subscribe to our channels to stay updated with the latest Laravel tips, tricks, and best practices: