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.