Laravel Eloquent Sum Multiple Columns Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent take() and skip() Query Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent whereRaw Condition Example
- Multiple orWhere Condition in Laravel Eloquent
- Laravel Eloquent Where Query Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- How to use Union Query with Laravel Eloquent?
- Laravel One to One Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial