The Arr::pluck() method is a convenient helper provided by Laravel array utilities to extract values from a multi-dimensional array or collection by a given key.
Example
<?php use Illuminate\Support\Arr; $array = [ ['website' => ['id' => 1, 'url' => 'reddit.com']], ['website' => ['id' => 2, 'url' => 'twitter.com']], ['website' => ['id' => 3, 'url' => 'dev.to']], ]; $urls = Arr::pluck($array, 'website.url'); // Result: ['reddit.com', 'twitter.com', 'dev.to'] ?>
Key Features in Laravel 11.x & Laravel 12.x
Dot Notation Support: You can use dot notation to access nested values.
Custom Key Assignment: Optionally specify a key to use for the resulting array.
<?php $keyedUrls = Arr::pluck($array, 'website.url', 'website.id'); // Result: [1 => 'reddit.com', 2 => 'twitter.com', 3 => 'dev.to'] ?>
Handling Missing Values: If a key doesn’t exist, null will be returned for that item.
Collection Compatibility: Works seamlessly with Laravel Collections as well.