From dbd36e9d6ce1372c26b11934aba0ba9d293b6eae Mon Sep 17 00:00:00 2001 From: Xurshudyan Date: Sat, 5 Jul 2025 13:33:08 +0400 Subject: [PATCH 1/3] Allow iteration over Fluent --- src/Illuminate/Support/Fluent.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Fluent.php b/src/Illuminate/Support/Fluent.php index e420711b3fa3..81622e1e745d 100755 --- a/src/Illuminate/Support/Fluent.php +++ b/src/Illuminate/Support/Fluent.php @@ -3,12 +3,15 @@ namespace Illuminate\Support; use ArrayAccess; +use ArrayIterator; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\InteractsWithData; use Illuminate\Support\Traits\Macroable; +use IteratorAggregate; use JsonSerializable; +use Traversable; /** * @template TKey of array-key @@ -17,7 +20,7 @@ * @implements \Illuminate\Contracts\Support\Arrayable * @implements \ArrayAccess */ -class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable +class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable, IteratorAggregate { use Conditionable, InteractsWithData, Macroable { __call as macroCall; @@ -245,6 +248,16 @@ public function offsetUnset($offset): void unset($this->attributes[$offset]); } + /** + * Get an iterator for the attributes. + * + * @return ArrayIterator + */ + public function getIterator(): Traversable + { + return new ArrayIterator($this->attributes); + } + /** * Handle dynamic calls to the fluent instance to set attributes. * From 0f0e92f48f755fe4dffc4afbd2b3b8172122773a Mon Sep 17 00:00:00 2001 From: Xurshudyan Date: Sat, 5 Jul 2025 13:37:34 +0400 Subject: [PATCH 2/3] Add test --- tests/Support/SupportFluentTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Support/SupportFluentTest.php b/tests/Support/SupportFluentTest.php index 86b5358c50a5..a81fd694e0b8 100755 --- a/tests/Support/SupportFluentTest.php +++ b/tests/Support/SupportFluentTest.php @@ -452,6 +452,25 @@ public function testMacroable() 'baz' => 'zal', ], $fluent->foo()->all()); } + + public function testFluentIsIterable() + { + $fluent = new Fluent([ + 'name' => 'Taylor', + 'role' => 'admin', + ]); + + $result = []; + + foreach ($fluent as $key => $value) { + $result[$key] = $value; + } + + $this->assertSame([ + 'name' => 'Taylor', + 'role' => 'admin', + ], $result); + } } class FluentArrayIteratorStub implements IteratorAggregate From 008d77507c91b59f5e81d5557ca52506e8ed7376 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 7 Jul 2025 09:17:11 -0500 Subject: [PATCH 3/3] Update Fluent.php --- src/Illuminate/Support/Fluent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Fluent.php b/src/Illuminate/Support/Fluent.php index 81622e1e745d..3c2273c4c71d 100755 --- a/src/Illuminate/Support/Fluent.php +++ b/src/Illuminate/Support/Fluent.php @@ -20,7 +20,7 @@ * @implements \Illuminate\Contracts\Support\Arrayable * @implements \ArrayAccess */ -class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable, IteratorAggregate +class Fluent implements Arrayable, ArrayAccess, IteratorAggregate, Jsonable, JsonSerializable { use Conditionable, InteractsWithData, Macroable { __call as macroCall;