Laravel Tutorials

Handle Nested Arrays Elegantly with Laravel's fluent() Helper

Published
Handle Nested Arrays Elegantly with Laravel's fluent() Helper image

Laravel introduces the fluent() helper function for smoother multi-dimensional array handling, providing an intuitive approach to access and manipulate nested data structures.

Work with multi-dimensional arrays using the fluent helper:

$config = [
    'app' => [
        'name' => 'TaskManager',
        'settings' => [
            'timezone' => 'UTC',
            'locale' => 'en',
        ]
    ],
    'features' => [
        ['name' => 'Analytics'],
        ['name' => 'Reports']
    ]
];

fluent($config)->app;
fluent($config)->get('app.name');
fluent($config)->collect('features')->pluck('name');
fluent($config)->scope('app.settings')->toJson();

Here's a practical implementation for an API response builder:

class ApiResponseBuilder extends Controller
{
    public function buildResponse(Product $product)
    {
        $responseData = [
            'product' => [
                'details' => [
                    'title' => $product->title,
                    'price' => $product->price,
                    'created' => $product->created_at
                ],
                'options' => [
                    'variants' => $product->variant_settings,
                    'shipping' => $product->shipping_options
                ]
            ],
            'metrics' => [
                'views' => $product->views->count(),
                'orders' => $product->orders->count(),
                'reviews' => $product->reviews->count()
            ]
        ];

        $response = fluent($responseData);

        return response()->json([
            'title' => $response->get('product.details.title'),
            'price' => $response->get('product.details.price'),
            'shipping' => $response->get('product.options.shipping'),
            'analytics' => $response->scope('metrics')->toArray(),
            'variants' => $response
                ->collect('product.options.variants')
                ->filter(fn($available) => $available)
                ->keys()
        ]);
    }
}

The fluent helper makes complex array operations more readable and maintainable.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article