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

[10.x] Add whenAggregated method to ConditionallyLoadsAttributes trait #47417

Merged
merged 5 commits into from Jun 16, 2023

Conversation

akr4m
Copy link
Contributor

@akr4m akr4m commented Jun 12, 2023

This PR introduces a new method, whenAveraged(), to the ConditionallyLoadsAttributes trait. This function is designed to work similarly to whenCounted(), but retrieves a relationship's column average value if it exists, expanding upon the trait's conditional loading capabilities.

Eloquent example with withAvg

$endpoints = $currentSite
            ->endpoints()
            ->withAvg('checks', 'response_time')
            ->get();

Eloquent Resource example with additional conditional relationship (whenAveraged)

public function toArray($request)
{
    return [
       'response_time_avg' => $this->whenAveraged('checks', 'response_time'),
    ];
}

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().

@akr4m
Copy link
Contributor Author

akr4m commented Jun 12, 2023

The eloquent also provides withMin, withMax, withSum, and withExists methods alongside withCount. And these methods place a {relation}_{function}_{column} attribute result.

So, in Eloquent Resources, some additional Conditional Relationships are sometimes necessary.

@akr4m akr4m changed the title Add whenAveraged method to ConditionallyLoadsAttributes trait [10.x] Add whenAveraged method to ConditionallyLoadsAttributes trait Jun 12, 2023
@taylorotwell
Copy link
Member

Maybe this should depend on a more generic whereAggregated method to prevent future PRs for basically the same thing but for sum, min, max, etc.?

Please mark as ready for review if you want me to take another look.

@taylorotwell taylorotwell marked this pull request as draft June 13, 2023 20:19
Copy link
Contributor Author

@akr4m akr4m left a 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.

@akr4m akr4m marked this pull request as ready for review June 13, 2023 20:52
@akr4m akr4m changed the title [10.x] Add whenAveraged method to ConditionallyLoadsAttributes trait [10.x] Add whenAggregated method to ConditionallyLoadsAttributes trait Jun 13, 2023
@taylorotwell
Copy link
Member

Could use a test or two?

@taylorotwell taylorotwell marked this pull request as draft June 14, 2023 20:32
@akr4m akr4m marked this pull request as ready for review June 16, 2023 12:02
@akr4m
Copy link
Contributor Author

akr4m commented Jun 16, 2023

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.

@taylorotwell taylorotwell merged commit f42ae50 into laravel:10.x Jun 16, 2023
15 of 16 checks passed
lovePizza added a commit to lovePizza/framework that referenced this pull request Dec 30, 2023
laravel#47417 intoduced whenAggregated method to ConditionallyLoadsAttributes trait but it returns null instead of MissingValue when the default value is not specified
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.

None yet

2 participants