I have this code:
DB::table('product_list')->select(DB::raw('(value*quantity) as total'))->get();
and this is my DB:
product = id,quantity,value,color
And I want them to be added all like:
1|10|500|blue
2|20|250|red
total for each = value*quantity
total = 10000
But it gives me:
array(2) { [0]=> object(stdClass)#266 (1) { ["total"]=> int(5000) } [1]=> object(stdClass)#263 (1) { ["total"]=> int(5000) } }
And the only way to sum it all is by running a foreach loop in PHP but I need to do it directly from the DB. But there must be another way, doesn't it?
Use the SQL SUM
function:
$total = DB::table('product_list')
->selectRaw('SUM(value * quantity) as total')
->pluck('total');
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다