Friday, November 14, 2025
HomeLanguagesLaravel Eloquent withSum() and withCount() Tutorial

Laravel Eloquent withSum() and withCount() Tutorial

Laravel eloquent query withSum and withCount of related table. In this example, you will learn eloquent withcount and withsum().

Now, this tutorial will show you example of how to use withSum() and withCount() with laravel relationship eloquent using Category and Product table.

As well as, you can use withSum() and withCount() with laravel 6, laravel 7 and laravel 8 version.

Laravel Eloquent withsum and withcount with relationships tutorial

Category Model:

First of all, create category model and add the following code into it:

<?php

  

namespace App\Models;

  

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

  

class Category extends Model

{

    use HasFactory;

  

    /**

     * Get the comments for the blog post.

     */

    public function products()

    {

        return $this->hasMany(Product::class);

    }

}

Product Model:

Now, create product model and add the following code into it:

<?php

  

namespace App\Models;

 

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

  

class Product extends Model

{

    use HasFactory;

  

    protected $fillable = [

        'name', 'price'

    ];

}

Laravel relationship with withSum() Example:

You can use withSum() eloquent in laravel as follow:

<?php

  

namespace App\Http\Controllers;

  

use App\Models\Category;

  

class MyController extends Controller

{

    /**

     * Write code on Method

     *

     * @return response()

     */

    public function index()

    {

        $categories = Category::select("id", "name")

                        ->withSum('products', 'price')

                        ->get()

                        ->toArray();

 

        dd($categories);

    }

}

Result:

Array

(

    [0] => Array

        (

            [id] => 1

            [name] => Mobile

            [products_sum_price] => 330

        )

    [1] => Array

        (

            [id] => 2

            [name] => Laptop

            [products_sum_price] => 410

        )

)

Laravel relationship with withCount() Example:

You can use withCount() eloquent in laravel as follow:

<?php

  

namespace App\Http\Controllers;

  

use App\Models\Category;

  

class MyController extends Controller

{

    /**

     * Write code on Method

     *

     * @return response()

     */

    public function index()

    {

        $categories = Category::select("id", "name")

                        ->withCount('products')

                        ->get()

                        ->toArray();

 

        dd($categories);

    }

}

Result:

Array

(

    [0] => Array

        (

            [id] => 1

            [name] => Mobile

            [products_count] => 3

        )

    [1] => Array

        (

            [id] => 2

            [name] => Laptop

            [products_count] => 2

        )

)

Recommended Laravel Tutorials

Recommended:- Laravel Try Catch

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS