ItSolutionStuff.com

Laravel Eloquent withSum() and withCount() Example

By Hardik Savani • April 16, 2024
Laravel

Hi All,

This article will provide some of the most important example laravel eloquent withsum. i explained simply step by step laravel eloquent withcount. step by step explain laravel withSum() example. you will learn laravel withCount() example.

Here i will give you very simple example of how to use withSum() and withCount() with laravel relationship eloquent. we will create Category and Product and how you can add relationship and get it.

you can easily use withSum() and withCount() with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Let's see example with output:

Category Model:

<?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:

<?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'

];

}

withSum() Example:

<?php

namespace App\Http\Controllers;

use App\Models\Category;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

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

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

->get()

->toArray();

dd($categories);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_sum_price] => 330

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_sum_price] => 410

)

)

withCount() Example:

<?php

namespace App\Http\Controllers;

use App\Models\Category;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

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

->withCount('products')

->get()

->toArray();

dd($categories);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Mobile

[products_count] => 3

)

[1] => Array

(

[id] => 2

[name] => Laptop

[products_count] => 2

)

)

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent Delete Record By ID Example

Read Now →

Delete All Records from Table in Laravel Eloquent

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Eloquent whereBetween() Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel One to One Eloquent Relationship Tutorial

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →