how i can use withSum that should be eager loaded on every query like withCount #44515
Answered
by
kverstae
samehdoush
asked this question in
General
-
ex : User Model
I need it on every query |
Beta Was this translation helpful? Give feedback.
Answered by
kverstae
Oct 9, 2022
Replies: 1 comment
-
A possible solution would be to add a global query scope (https://laravel.com/docs/9.x/eloquent#global-scopes) to your user model (or whatever model you want). // CountAndSumScope
class CountAndSumScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->withCount([...])->withSum([...]);
}
}
// User
protected static function booted()
{
static::addGlobalScope(new CountAndSumScope);
} This code is untested, but I think this idea could work |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
samehdoush
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A possible solution would be to add a global query scope (https://laravel.com/docs/9.x/eloquent#global-scopes) to your user model (or whatever model you want).
This would look something like this:
This code is untested, but I think this idea could work