- Laravel Version: 8.61.0
- PHP Version: 7.4.22
- Database Driver & Version: MySQL v8
Description:
When a LazyCollection instance is passed to Blade view and looped via @foreach directive, it causes the query to execute twice. Believe that its because of how foreach directive is compiled under the hood and how count and last record of the Countable is managed inside ManagesLoops file.
Steps To Reproduce:
- Inside controller or route file
$users = User::lazy();
return view('welcome', ['users' => $users]);
@foreach($users as $user)
<div>{{ $user->name }}</div>
@endforeach
- Queries detected using Ray

GitHub Repo
https://github.com/awebartisan/lazy-collection-blade-query-bug