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
Contributor

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

This comment has been minimized.

Copy link
Member

commented Sep 6, 2019

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

@GrahamCampbell

This comment has been minimized.

Copy link
Member

commented Sep 6, 2019

What about strict() as a method name?

@JosephSilber

This comment has been minimized.

Copy link
Contributor Author

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.

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

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

This comment has been minimized.

Copy link
Contributor

commented Sep 7, 2019

Would snapshot() be a clearer name?

@thecrypticace

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

commented Sep 18, 2019

Definitely prefer eager.

@JosephSilber JosephSilber force-pushed the JosephSilber:cache-method branch from 3f11fce to a2c2682 Sep 18, 2019
@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

This comment has been minimized.

Copy link
Contributor Author

commented Sep 18, 2019

Updated.

@taylorotwell taylorotwell merged commit ba6e0fd into laravel:6.x Sep 25, 2019
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
cangelis added 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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.