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

[5.4] Add fresh method on Eloquent\Collection. #19616

Merged
merged 3 commits into from
Jun 17, 2017

Conversation

mathieutu
Copy link
Contributor

Hi,
This PR adds this simple handy method which permit to fresh all the models in an Eloquent collection.
It also adds two tests: one for the actual Model::fresh() method, and one for the brand new Collection::fresh() one.

Wait for your comments (or your merge :P)!
Matt'

@decadence
Copy link
Contributor

Isn't this failed on empty Collection since first returns null?

@mathieutu
Copy link
Contributor Author

@decadence can you explain a bit more your issue? (some code?)

*/
public function fresh($with = [])
{
$model = $this->first();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no code. I just assume if Collection is empty this line returns null and next code will fail with Trying to get property of non-object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah! I understand now.
But why would you want to fresh an empty collection ? It's about loading all models from database, if you have no model, no need to fresh it ?

But anyway you're right, I will fix it by a check with a isEmpty.

Thank's!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add $users = new Collection(); in your test before assertion and it will fail with Error: Call to a member function newQueryWithoutScopes() on null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add in start of the method something like:

if ($this->isEmpty()) {
    return new static; // or return $this;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't see your comment and already did PR.
I don't need fresh on empty Collection personally but this check has to be there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes you can't say if Collection will be empty or not. Therefore users have to do it check manually which is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@decadence Yeah I agree, but be careful, you made the PR on 5.5 version, and not 5.4!
This one was for 5.4...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I think this was for 5.5 because it was there already. Maybe it's worth to make it for 5.4 too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's done (and I've updated tests too).

mathieutu added a commit to mathieutu/framework that referenced this pull request Jun 19, 2017
* commit '05060b183dd09cee6dce498da5afde9e66fd1a29':
  Apply fixes from StyleCI (laravel#19659)
  [5.4] Add ability to remove a global scope with another global scope (laravel#19657)
  [5.4] Add fresh method on Eloquent\Collection. (laravel#19616)
  support multiple fields to validateDifferent (laravel#19637)
  escape default value of yield blade directive (laravel#19643)
  [5.4] Add "avg" and "average" to higher order proxy. (laravel#19628)
  tagged v5.4.27 release notes
  version
  Add missing dependency. (laravel#19623)
  update timestamp on soft delete only when its used (laravel#19627)
  Add new `diffAssoc()` method to collection (laravel#19604)
  Fix tests code coverage config. (laravel#19609)

# Conflicts:
#	src/Illuminate/Database/Eloquent/Collection.php
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.

3 participants