Laravel's Fluent Class Goes Iterable: Cleaner Loops Ahead

Laravel's Fluent Class Goes Iterable: Cleaner Loops Ahead

The Fluent class in Laravel now implements the Iterable contract, eliminating the need for toArray() calls in foreach loops. It's a small change that makes a big difference in code cleanliness.

From Verbose to Natural

Previously, iterating over Fluent objects required an explicit toArray() call, which felt unnecessary and broke the natural flow of working with these objects:

// The old way - required toArray() call
foreach ($user->settings->toArray() as $key => $value) {
    echo "{$key}: {$value}\n";
}

Now, Fluent objects can be used directly in loops, making the code more intuitive and cleaner:

// The new way - direct iteration
foreach ($user->settings as $key => $value) {
    echo "{$key}: {$value}\n";
}

Real-World Example

Consider a simple user preferences system where you need to display and process various settings:

<?php

namespace App\Models;

use Illuminate\Support\Fluent;

class UserPreferences
{
    public function getSettings(): Fluent
    {
        return new Fluent([
            'theme' => 'dark',
            'language' => 'en',
            'notifications' => true,
            'timezone' => 'UTC'
        ]);
    }

    public function displaySettings(): array
    {
        $settings = $this->getSettings();
        $display = [];

        // Clean iteration without toArray()
        foreach ($settings as $key => $value) {
            $display[] = ucfirst($key) . ': ' . ($value ? 'Yes' : 'No');
        }

        return $display;
    }

    public function countEnabledFeatures(): int
    {
        $settings = $this->getSettings();
        $count = 0;

        foreach ($settings as $setting) {
            if ($setting === true) {
                $count++;
            }
        }

        return $count;
    }
}

// Usage example
$preferences = new UserPreferences();
$settings = $preferences->getSettings();

// Direct iteration works now
foreach ($settings as $key => $value) {
    echo "Setting: {$key} = {$value}\n";
}

In Blade templates, the improvement is equally noticeable:

{{-- Before: needed toArray() --}}
@foreach($user->settings->toArray() as $key => $value)
    <li>{{ $key }}: {{ $value }}</li>
@endforeach

{{-- After: direct iteration --}}
@foreach($user->settings as $key => $value)
    <li>{{ $key }}: {{ $value }}</li>
@endforeach

The Iterable contract implementation makes Fluent objects behave more like native PHP arrays, reducing friction and making Laravel's fluent interface more intuitive to work with. This small enhancement removes a common pain point when working with configuration objects and API responses.


Stay Updated with More Laravel Tips

Enjoyed this article? There's plenty more where that came from! Subscribe to our channels to stay updated with the latest Laravel tips, tricks, and best practices:

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.
jamie@example.com
Subscribe