diff --git a/src/Illuminate/View/Concerns/ManagesLoops.php b/src/Illuminate/View/Concerns/ManagesLoops.php index edd6363ec7d3..cb7ccde35f37 100644 --- a/src/Illuminate/View/Concerns/ManagesLoops.php +++ b/src/Illuminate/View/Concerns/ManagesLoops.php @@ -2,8 +2,8 @@ namespace Illuminate\View\Concerns; -use Countable; use Illuminate\Support\Arr; +use Illuminate\Support\LazyCollection; trait ManagesLoops { @@ -22,7 +22,9 @@ trait ManagesLoops */ public function addLoop($data) { - $length = is_array($data) || $data instanceof Countable ? count($data) : null; + $length = is_countable($data) && ! $data instanceof LazyCollection + ? count($data) + : null; $parent = Arr::last($this->loopsStack); diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index a3b2a316c2cd..dddc7f261464 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -11,6 +11,7 @@ use Illuminate\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\HtmlString; +use Illuminate\Support\LazyCollection; use Illuminate\View\Compilers\CompilerInterface; use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\Engines\EngineResolver; @@ -702,6 +703,30 @@ public function testAddingUncountableLoop() $this->assertEquals([$expectedLoop], $factory->getLoopStack()); } + public function testAddingLazyCollection() + { + $factory = $this->getFactory(); + + $factory->addLoop(new LazyCollection(function () { + $this->fail('LazyCollection\'s generator should not have been called'); + })); + + $expectedLoop = [ + 'iteration' => 0, + 'index' => 0, + 'remaining' => null, + 'count' => null, + 'first' => true, + 'last' => null, + 'odd' => false, + 'even' => true, + 'depth' => 1, + 'parent' => null, + ]; + + $this->assertEquals([$expectedLoop], $factory->getLoopStack()); + } + public function testIncrementingLoopIndices() { $factory = $this->getFactory();