-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[8.x] Add withColumn() to QueriesRelationships to support min, max, sum and… #34965
Conversation
This PR unfortunately contains breaking changes to a method signature. It also contains unrelated style changes to docblocks etc. Please send to master. |
@driesvints /**
* Add subselect queries to count the relations.
*
* @param mixed $relations
* @return $this
*/
public function withCount($relations)
{
return $this->withColumn(is_array($relations) ? $relations : func_get_args(), '*', 'count');
} Just the body of the method has been changed. @taylorotwell |
@khalilst right, thanks for clarifying. |
… avg similar to withCount
@khalilst did you follow this for this implementation? https://github.com/dwightwatson/aggregate/blob/master/src/AggregateServiceProvider.php |
@taylorotwell I addition to those methods and support for other aggregation methods, I have add capability to return single column without aggregation which are not available in your given link. |
OK, after reviewing the implementation by @dwightwatson do you see any problems with this implementation? |
@taylorotwell |
@khalilst this PR forgot to add |
@taylorotwell |
hi @khalilst, @taylorotwell, the new withCount has broken our app. We are using withCount inside a globalScope: public static function bootHasAudiences(): void
{
static::addGlobalScope(
'has_audiences',
function (Builder $query): void {
// sort by audience
$query->withCount('audiences')->orderBy('audiences_count', 'desc');
}
);
}
}
public function audiences()
{
return $this->belongsToMany(Audience::class)->using(AudiencePivot::class);
} Error: Maximum function nesting level of '256' reached, aborting! before this merge, everything was working correctly. |
@kanidjar |
@khalilst let me know soon if you see the problem / fix - otherwise I will have to revert everything. |
@taylorotwell |
@taylorotwell I tried different projects with the given code by @kanidjar and I have seen no problem. @kanidjar You have edited your code several times. Are you sure there was not missing code here? |
@khalilst here it is: https://gist.github.com/kanidjar/5594c42ced9b7494239bf28733f243a0 I've edited the code to remove all unrelated code. A simple Item::all() with the global scope enabled is enough. |
@kanidjar Thank you. I'm trying to find out the problem. |
@taylorotwell @kanidjar I found the problem raised from line 385 at QueriesRelationships.php. I corrected this line and commit to open related PR #35029. |
Thank you very much for investigating. So, according to you, the issue comes from this PR #35004 |
@khalilst we need the fix in a separate PR. Also, why does that cause the problem. The change @sebastiaanluca made is pretty valid that we would want to wrap the column names. |
@taylorotwell I can confirm that applying the fix locally fixes the issue. |
@taylorotwell Please take a look at the stack trace given by @kanidjar here. @kanidjar Yes, you are right. |
We can call $this->getQuery()->getGrammar()->wrap() instead. |
Pushed to 8.x. Please remove your fix from this from your other PR. |
@taylorotwell Yes, it does. I'm gonna remove the bugfix from other PR. |
Released as 8.12.3. |
The $posts = App\Models\Post::withCount(['votes', 'comments' => function (Builder $query) {
$query->where('content', 'like', 'foo%');
}])->get(); |
@robsontenorio Post::withMax(['votes', 'comments' => function (Builder $query) {
$query->where('content', 'like', 'someting%');
}], 'column_name')->get(); Note: If you are using |
@khalilst thank you! The trick was second param |
@robsontenorio You are welcome. I will create a PR to Laravel Doc asap. |
@taylorotwell |
@Alexmg86 sometimes we reconsider feature requests. Don't be disappointed that your PR wasn't accepted at the time. Be happy that it's now finally in the core of the framework. |
@Alexmg86 also, looking at your PR it wasn't closed because we didn't accept it. It was closed because it lacked a thorough explanation of the feature and tests. |
@driesvints But you could have said it, but a person just stole my work and this is at least bad. I'm really very upset |
Can you point out which parts the contributor stole from you? |
@driesvints Methods related to aggregation withMax, withMin, withSum, withAvg. You can see the dates of my commits, ideas. |
Please link to them. |
@driesvints how to do it? |
@Alexmg86 select the links to the pieces of your code and paste them here so we can compare them. |
@Alexmg86 I have never seen your code and I can not understand your insulting words without any proof. |
@khalilst ok. I think so. Maybe you are right |
@Alexmg86 Sure, but definitely you were wrong |
I'm going to close this off because I believe nothing more constructive can come out of this discussion. |
I add a method named
withColumn
(copied/edited fromwithCount
) method to support more SQL aggregation functions likemin, max, sum, avg
over relationships, e.g.Also, the
withCount
methods useswithColumn
to avoid duplicated code and it works with backward-compatibility.However, it is possible to call other/custom SQL function using
withColumn
, e.g.In addition to, it is possible to add relationship's column to selected columns without SQL function, e.g.
Both are working. Please consider the sub-query limited to have only one result.