I have a User model, who hasMany categories and category hasMany posts and post hasOne Product
Now I want to sum all of the product->price of Auth::user()
how can i do that withSum
or withCount
function. in Laravel
Since your question doesnt present any details, I will assume that you have all the relations well set up.
You can get the sum using the relations like this
$sum = Product::query()
->whereHas('post', function($post) {
$post->whereHas('category', function($category) {
$category->whereHas('user', function($user) {
$user->where('id',auth()->id());
});
});
})
->sum('price');