-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[10.x] Add whenAggregated
method to ConditionallyLoadsAttributes
trait
#47417
Conversation
The eloquent also provides So, in Eloquent Resources, some additional Conditional Relationships are sometimes necessary. |
whenAveraged
method to ConditionallyLoadsAttributes
traitwhenAveraged
method to ConditionallyLoadsAttributes
trait
Maybe this should depend on a more generic Please mark as ready for review if you want me to take another look. |
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.
I've made updates based on the feedback received. The initial whenAveraged
method has been replaced with a more generic whenAggregated
method. This new method can handle multiple aggregate functions including min
, max
, avg
, sum
, exists
, and more.
The whenAggregated
method takes an additional parameter, $aggregate
, which specifies the type of aggregation to perform. The attribute is then generated by appending an underscore, the aggregate function, and another underscore to the snake-cased version of the relationship name, followed by the column name.
whenAveraged
method to ConditionallyLoadsAttributes
traitwhenAggregated
method to ConditionallyLoadsAttributes
trait
Could use a test or two? |
Absolutely, I've added two tests along with the resource implementation. Please take a look and let me know if any adjustments are needed. |
laravel#47417 intoduced whenAggregated method to ConditionallyLoadsAttributes trait but it returns null instead of MissingValue when the default value is not specified
This PR introduces a new method,
whenAveraged()
, to theConditionallyLoadsAttributes
trait. This function is designed to work similarly towhenCounted()
, but retrieves a relationship's column average value if it exists, expanding upon the trait's conditional loading capabilities.Eloquent example with withAvg
Eloquent Resource example with additional conditional relationship (
whenAveraged
)The
whenAveraged()
method takes in a relationship, a column name, an optional value, and an optional default value. It generates an attribute name by appending the string 'avg' to the snake-cased version of the relationship name, followed by the column name. If this attribute does not exist in the resource's attributes, it returns the default value (or null if no default is provided).This new method is particularly useful when dealing with large datasets where you want to conditionally load averages. It provides a convenient and readable way to handle such scenarios directly in the resource class, expanding upon the conditional loading capabilities of
whenCounted()
.