Get Sum of Column Values in Laravel with Example

Examples to get sum of column values in laravel

Before getting started with this, we have to create a table in the database. So, let’s create a table

$productdata = DB::table('product_info')->select(DB::raw('SUM(quantity*price) as amount'))->get(); foreach($productdata as $product)
{ $amount = $product->amount;
//amount with where condition $productdata_using_where = DB::table('product_info')->select(DB::raw('SUM(quantity*price) as amount'))->where('quantity','>',3)->get(); foreach($productdata_using_where as $value)
$amount_using_where = $value->amount;



Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store