[9.x] Standardize withCount() & withExists() eager loading aggregates. - #41914
Merged
Conversation
withExists() eager loading aggregates.withCount() & withExists() eager loading aggregates.
Contributor
|
@taylorotwell This is actually a breaking change because the example from the docs no longer works now use Illuminate\Database\Eloquent\Builder;
$posts = Post::withCount(['votes', 'comments' => function (Builder $query) {
$query->where('content', 'like', 'code%');
}])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;will throw |
r-kujawa
added a commit
to r-kujawa/framework
that referenced
this pull request
Apr 12, 2022
r-kujawa
added a commit
to r-kujawa/framework
that referenced
this pull request
Apr 12, 2022
r-kujawa
added a commit
to r-kujawa/framework
that referenced
this pull request
Apr 12, 2022
withCount() & withExists() eager loading aggregates.withCount() & withExists() eager loading aggregates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
withCount()&withExists()aggregates.Why do we need this?
Before this PR
There are 3 different ways to pass parameters in
with...()functions:is_array($relations) ? $relations : func_get_args()).If we compare the
with(),withCount()&withExistsfunctions, this is what each of them currently support:with(): 1, 2 & 3.withCount(): 1 & 2.withExists(): 1 (with only 1 relation 😞) & 2.The reason why I am writing this PR is because I ran into a situation where I supposed that since I was able to do it with one of these methods, why shouldn't I be able to do the same with any of the other 2 methods (that's where I got disappointed 😞).
I did notice that it would probably be best practice and most efficient to have way 1 & 3 removed and only support way 2. But that would result in some breaking changes (LMK if it sounds interesting and I will make a PR for 10.x).
With this PR
The 3 functions mentioned above will support all 3 different ways to pass the params.
🔴 The only caveat I might see is that the
with()function includes a second parameter in it's signature (@param string|\Closure|null $callback) while the other 2 don't.