How to Select with Sum Query in Laravel?

Most of us mostly need to get the sum of few numbers ,visitor,or others in laravel application. Today now in this post i will show you How to Select with Sum Query in Laravel? We can easily get the sum of column by using mysql SUM(). So we have two way to get the sum of any column value.At first one is we can do by use laravel sum() of query builder and another one is do directly with select statement by using DB::raw().

Here I will give you both example. So we can see and also use any one as perfect for us.

Example 1:
$data = DB::table("click")->sum('numberofclick');
Example 2:
$data = DB::table("click")
	    ->select(DB::raw("SUM(numberofclick) as count"))

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook.


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →