Discover the Power of eachSpread(): A Hidden Laravel Collection Gem

Laravel devs, here's a gem for you: πŸ’Ž The Laravel framework continues to offer powerful tools to make your development process more efficient and enjoyable. One such lesser-known but highly useful method is eachSpread(). This method allows you to handle nested arrays by spreading their elements into individual parameters effortlessly. Let's explore how to use this feature with a real-life example.

What is eachSpread()?

The eachSpread() method is part of Laravel Collections and is designed to iterate over a collection of arrays, spreading each nested array's elements into individual parameters for the provided callback. This can be particularly useful when dealing with collections containing tuples or arrays that need to be processed individually.

Why Use eachSpread()?

  1. Ease of Use:
    • Simplifies the process of working with nested arrays.
    • Makes your code cleaner and more readable.
  1. Efficient Data Handling:
    • Useful for transforming and processing collections containing multiple arrays.
  1. Real-Life Applications:
    • Ideal for scenarios where you need to handle paired or grouped data, such as coordinates, key-value pairs, or any data structured in tuples.

Real-Life Example: Processing Order Shipments

Imagine you are managing a collection of order shipments, each represented by an array containing an order ID and a shipment status. You want to process each order and update its status accordingly.

Example Code

// Example order shipments collection
$shipments = collect([
    [101, 'shipped'],
    [102, 'pending'],
    [103, 'delivered'],

$shipments->eachSpread(function ($orderId, $status) {
    echo "Order ID: $orderId, Status: $status\n";

    // Example processing logic (updating order status in a database, etc.)
    // Order::find($orderId)->update(['status' => $status]);

// Output:
// Order ID: 101, Status: shipped
// Order ID: 102, Status: pending
// Order ID: 103, Status: delivered

In this example, we have a collection of order shipments, each containing an order ID and a status. Using the eachSpread() method, we can easily iterate over the collection and handle each order individually.


The eachSpread() method is a powerful addition to the Laravel Collection methods, allowing you to handle nested arrays with ease and clarity. By leveraging this method, you can write cleaner, more efficient code and tackle complex data processing tasks with simplicity.

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.