Skip to content

[12.x] Make Fluent class iterable#56218

Merged
taylorotwell merged 3 commits into
laravel:12.xfrom
xurshudyan:fluent-iterable
Jul 7, 2025
Merged

[12.x] Make Fluent class iterable#56218
taylorotwell merged 3 commits into
laravel:12.xfrom
xurshudyan:fluent-iterable

Conversation

@xurshudyan

@xurshudyan xurshudyan commented Jul 5, 2025

Copy link
Copy Markdown
Contributor

Currently, while Fluent supports array-style access (ArrayAccess) and dynamic property access, it cannot be directly iterated. This gap often causes inconvenience in Laravel applications, especially when working with Eloquent attribute casting such as:

protected function casts(): array
{
    return [
        'settings' => \Illuminate\Database\Eloquent\Casts\AsFluent::class
    ];
}

Without this enhancement, developers must call ->toArray() before iterating over casted attributes

foreach ($user->settings->toArray() as $key => $value) {
    // 
}

By enabling native iteration, this change simplifies and cleans up common usage patterns, allowing

foreach ($user->settings as $key => $value) {
    //
}

@taylorotwell taylorotwell merged commit 4c7e519 into laravel:12.x Jul 7, 2025
58 of 60 checks passed
mohammad-fouladgar pushed a commit to mohammad-fouladgar/framework that referenced this pull request Jul 22, 2025
* Allow iteration over Fluent

* Add test

* Update Fluent.php

---------

Co-authored-by: Xurshudyan <v.khurshudyan@aico.swiss>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants