-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Allow use of associative arrays as only argument to withAggregate, withMin, etc. #35073
Conversation
* | ||
* @param mixed $relations | ||
* @param string $column | ||
* @param string $function | ||
* @return $this | ||
*/ | ||
public function withAggregate($relations, $column, $function = null) | ||
public function withAggregate($relations, $column = null, $function = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@driesvints Thanks for looking into this. I'm sorry if I was on the wrong track here. I assumed making a required parameter optional would not be considered a breaking change. I think that nothing would actually break if it kept using the current syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@driesvints Got it. Thank you very much for clarifying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@driesvints Should I change the title to [9.x]? Sorry, I just started contributing recently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to submit a new PR to the master branch for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to update the phpdoc too
* @param string $column | ||
* @return $this | ||
*/ | ||
public function withMax($relation, $column) | ||
public function withMax($relations, $column = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change to the method signature. All of these method signature changes below are breaking changes as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These methods did support arrays as the first parameter before, too. It was just not documented. But you might be more concerned about the second parameter becoming optional. As I mentioned in the comment above, nothing should break if it keeps using the former way implemented in #34965. But maybe I have a wrong understanding of what a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we just keep it with multiple calls for now. |
This PR allows the use of an associative array as the only argument for the new Builder methods
withAggregates
,withSum
, etc. introduced in #34965.It lets you specify the targeted column using a colon
relation:column
as seen inModel::with('relation:id,name')
.With the current implementation you need multiple calls if you want to sum over different columns:
With this change you may instead write
This also enables counting on a specific column using
withCount
Some more examples:
A note about optional and mandatory square brackets:
The implementation of
withCount
already differs from all the otherwithAggregate
,withSum
,withMin
, ... :To prevent a breaking change, these differences still remain and exist unchanged.
Steps taken:
$column
param ofwithAggregate()
to be nullparseWithRelations()
here as it trims of the:column
part of the array keys and calls unwantedselect()
andaddNestedWiths()
withAggregate
to a newapplyAggregateSubSelect()
to allow changing$column
,$function
,$alias
without side effects in the next iterationI added tests to
tests/Database/DatabaseEloquentBuilderTest.php
but no integration tests yet.I'd also like to kindly ask @khalilst for his thoughts on this, since he contributed a lot on this topic lately.