In my previous tutorial, I discuss laravel withSum and withSum with condition in laravel. Now we know how to write a query using withSum and how to make a condition with withSum query in laravel. But sometimes we need to calculate the sum with nested relation using withSum(). So in this tutorial, I will show you how to use laravel withsum nested in laravel eloquent query. So if you do not know the procedure of writing a query for fetching the sum of the nested relationship attribute. Then this laravel withsum nested tutorial is for you. I will use Post Comment and Reply model and both comments and replies tables contain the vote column.

I will create a hasManyThrough eloquent relationship to find this sum of comments and replies vote column combined. Let's see the example query of laravel withsum nested.

laravel-withsum-nested

Now let's see the update the Post model like:

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
    use HasFactory;

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function commentsAndReplies()
    {
        return $this->hasManyThrough(Reply::class,Comment::class);
    }
}

 

Now let's see the update the Comment model like:

app/Models/Comment.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use HasFactory;

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

 

Now see the example code of laravel withsum nested:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\Post;

class TutorialController extends Controller
{   
    /**
     * Tutorial from LARAVELIA
     */
    public function index()
    {   
        return Post::withSum('commentsAndReplies','vote')->get();
    }
}

 

Remember both comments and replies table has a vote column. Now see the output:

[
    {
        "id": 1,
        "user_id": 1,
        "title": "Omnis cupiditate sed quia tempora. Nulla eum debitis molestias porro.",
        "post_views": 12,
        "is_published": 1,
        "created_at": "2023-01-29T08:49:59.000000Z",
        "updated_at": "2023-01-29T08:49:59.000000Z",
        "comments_and_replies_sum_vote": "186"
    },
    {
        "id": 2,
        "user_id": 2,
        "title": "Enim sed pariatur veritatis animi. Odit quaerat beatae excepturi quaerat.",
        "post_views": 30,
        "is_published": 1,
        "created_at": "2023-01-29T08:49:59.000000Z",
        "updated_at": "2023-01-29T08:49:59.000000Z",
        "comments_and_replies_sum_vote": "26"
    }
]

 

Read also: Laravel WithSum Where Condition Query Example

 

Conclusion

In this laravel withsum nested tutorial,  I have tried my best to let you know how to write a query with laravel withsum nested in laravel 9. Hope now after completing this withsum nested laravel 9 query example tutorial, you will know laravel 9 eloquent withsum nested.