laravel sum query

Posted by w3diy - 5 months ago

How to get sum of two different columns with Laravel 6 Query Builder?

Example 1:

$products = DB::table("products")->sum('total_page_view');
print_r($products);

Example 2: Laravel 6 select with sum query example

$products = DB::table("products")
        ->select(DB::raw("SUM(total_page_view) as count"))
        ->orderBy("created_at")
        ->groupBy(DB::raw("year(created_at)"))
        ->get();
print_r($products);