Laravel Select with Sum Query Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

We mostly required to get sum of amount, salary etc in laravel. We can also get sum of column using mysql SUM(). We have two way to get sum of column value. first we can use laravel sum() of query builder and another one we can use with directly with select statement using DB::raw(). I give you both lesson you can see and use any one as perfect for you.

Lesson 1:

$data = DB::table("click")->sum('numberofclick');

print_r($data);

Lesson 2:

$data = DB::table("click")

->select(DB::raw("SUM(numberofclick) as count"))

->orderBy("created_at")

->groupBy(DB::raw("year(created_at)"))

->get();

print_r($data);

Recent Posts

Related Posts