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] Adds ability to eager load counts via property #19154

Merged
merged 3 commits into from
May 11, 2017
Merged

[5.4] Adds ability to eager load counts via property #19154

merged 3 commits into from
May 11, 2017

Conversation

regularlabs
Copy link
Contributor

This PR adds the ability to eager load counts via the protected property $withCount in the same way you can eager load relations with $with.
Like:

class FooBar extends \Illuminate\Database\Eloquent\Model
{
    protected $with      = ['foo', 'bar'];
    protected $withCount = ['baz', 'qux'];

    ...
}

This PR adds the ability to eager load counts via the protected property `$withCount` in the same way you can eager load relations with `$with`.
Like:
```
class FooBar extends \Illuminate\Database\Eloquent\Model
{
    protected $with      = ['foo', 'bar'];
    protected $withCount = ['baz', 'qux'];

    ...
}
```
@tillkruss tillkruss changed the title Adds ability to eager load counts via property [5.4] Adds ability to eager load counts via property May 11, 2017
@tillkruss
Copy link
Contributor

Tests are failing.

@regularlabs
Copy link
Contributor Author

To pass the checks I have also added an early return in the withCount method, so that it does not do anything to the query builder when the passed relations is empty.

@taylorotwell taylorotwell merged commit c4f589e into laravel:5.4 May 11, 2017
@regularlabs regularlabs deleted the patch-2 branch May 11, 2017 21:28
@sausin
Copy link
Contributor

sausin commented May 13, 2017

@regularlabs Thanks for this! 👍

I was not able to use nested relationships the same it works with ->with('first.second'). Is this a possibility? The name convention system could go follow the same setup and be something like first_second_count

The error that I get with the current implementation for the above example is:

BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::first.second()'

@decadence
Copy link
Contributor

@sausin It just uses withCount method which doesn't support nested relations.

@sausin
Copy link
Contributor

sausin commented May 13, 2017

@decadence Thanks for clearing it up! Should've checked the changes properly

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