ItSolutionStuff.com

Laravel Eloquent Sum Multiple Columns Example

By Hardik Savani • April 16, 2024
Laravel

Are you looking for an example of laravel eloquent sum multiple columns. you can see laravel query builder sum two columns. i explained simply about multiple sum in laravel query. i would like to share with you multiple column sum in laravel. Let's see bellow example laravel sum query multiple columns.

let's see simple examples of multiple fields sum in laravel. so you can easily use it with your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. so let's see bellow example that will helps you lot.

Example

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use DB;

class SignaturePadController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$products = Product::select(

"id",

"name",

DB::raw("SUM(sell) as total_sell"),

DB::raw("SUM(stock) as total_stock")

)

->groupBy("category_id")

->get();

dd($products->toArray());

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Silver

[total_sell] => 40

[total_stock] => 139

)

[1] => Array

(

[id] => 49

[name] => Apple

[total_sell] =>

[total_stock] => 3

)

[2] => Array

(

[id] => 52

[name] => Dell

[total_sell] =>

[total_stock] => 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 take() and skip() Query Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Eloquent whereNotBetween() Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

Multiple orWhere Condition in Laravel Eloquent

Read Now →

Laravel Eloquent Where Query Examples

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel One to One Eloquent Relationship Tutorial

Read Now →

Laravel Many to Many Eloquent Relationship Tutorial

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →