Arr::pull() Method in Laravel 11.x & Laravel 12.x

The Arr::pull() method is a convenient helper provided by Laravel’s array utilities. In Laravel 11.x, Laravel 12.x, this method works similarly to previous versions but with the framework’s ongoing improvements.

What Arr::pull() Does?

This method retrieves and removes a value from an array using “dot” notation:

<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::pull($array, 'products.desk.price');
?>

After this operation:

$price will be 100
The original $array will no longer contain the price element.

Key Features

1. Dot Notation Support: Access nested array elements using dot notation
2. Default Value: You can specify a default value if the key doesn’t exist
3. Modifies Original Array: Removes the pulled element from the original array

Usage Examples

<?php
// Basic usage
$array = ['name' => 'John', 'age' => 30];
$name = Arr::pull($array, 'name');
// $name = 'John', $array = ['age' => 30]
// With nested arrays
$array = ['user' => ['name' => 'Jane', 'email' => 'jane@example.com']];
$email = Arr::pull($array, 'user.email');
// $email = 'jane@example.com', $array = ['user' => ['name' => 'Jane']]
// With default value
$value = Arr::pull($array, 'key.that.doesnt.exist', 'default');
// $value = 'default'
?>

When to Use?

1. When you need to both retrieve and remove an array element.
2. When working with configuration arrays or request data.
3. When you need clean access to nested array elements.
4. The Arr::pull() method is part of Laravel’s Illuminate\Support\Arr helper class, which provides various array manipulation functions.