Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing LazyCollection to Blade view causes query to execute twice #39003

Closed
awebartisan opened this issue Sep 28, 2021 · 3 comments
Closed

Passing LazyCollection to Blade view causes query to execute twice #39003

awebartisan opened this issue Sep 28, 2021 · 3 comments

Comments

@awebartisan
Copy link
Contributor

  • 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]);
  • Inside blade view
    @foreach($users as $user)
        <div>{{ $user->name }}</div>
    @endforeach
  • Queries detected using Ray
    image

GitHub Repo

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

@driesvints
Copy link
Member

Yes, that's indeed how things work atm. I don't really see a bug here tbh.

@awebartisan
Copy link
Contributor Author

awebartisan commented Sep 28, 2021

@driesvints can it be a feature request? Where Blade compiler pays attention to specifics of LazyCollection and deal it differently than normal Collection?

@driesvints
Copy link
Member

Sure but this issue tracker isn't for feature requests. If you can, feel free to PR something 👍

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

No branches or pull requests

2 participants