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

[6.x] Add an eager() method to the lazy collection class #29832

Merged
merged 2 commits into from
Sep 25, 2019

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Sep 3, 2019

The eager() method enumerates all of the lazy collection's values, and constructs a new LazyCollection instance, which will now be backed by an array:

$users = User::cursor();

$users = $users->eager();

$count = $users->count();

$users->each(function ($user) {
    //
});

The above will only fetch the users once. Without calling eager in between, the query would execute (and fetch all results) twice.

This is a pretty useless example, but is a clear illustration of what the eager method does.

@JosephSilber JosephSilber changed the title Add a cache() method to the lazy collection class [6.x] Add a cache() method to the lazy collection class Sep 3, 2019
@taylorotwell
Copy link
Member

The naming of this and when to even use it are still very murky to me. 😄

@GrahamCampbell
Copy link
Member

What about strict() as a method name?

@JosephSilber
Copy link
Member Author

JosephSilber commented Sep 6, 2019

when to even use it are still very murky to me

Do you understand when a user would want to call lazy() on a regular collection? You may want to run lazy operations even on a list that is already all in memory.

eager is functionally equivalent to $lazyCollection()->collect()->lazy().

// These two end up doing the same thing:
$lazyCollection = $lazyCollection()->collect()->lazy();
$lazyCollection = $lazyCollection()->eager();

@BrandonSurowiec
Copy link
Contributor

BrandonSurowiec commented Sep 7, 2019

Would snapshot() be a clearer name?

@thecrypticace
Copy link
Contributor

thecrypticace commented Sep 8, 2019

Yeah, I'm not a fan of the name cache. It feels too tied to actual persistent caching mechanisms. Tbh, snapshot() isn't a bad name. It seems pretty descriptive to me. In my lazy collection implementation I called this method eager() (as you eagerly collect the values into the collection — you are making it less lazy) which is another option.

@taylorotwell
Copy link
Member

Definitely prefer eager.

@JosephSilber JosephSilber changed the title [6.x] Add a cache() method to the lazy collection class [6.x] Add an eager() method to the lazy collection class Sep 18, 2019
@JosephSilber
Copy link
Member Author

Updated.

@taylorotwell taylorotwell merged commit ba6e0fd into laravel:6.x Sep 25, 2019
cangelis pushed a commit to cangelis/framework that referenced this pull request Sep 25, 2019
…9832)

* Add a cache() method to the lazy collection class

* Rename `cache` method to `eager`
@JosephSilber JosephSilber deleted the cache-method branch April 26, 2020 18:53
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.

5 participants