Hedam
Hedam

Reputation: 2249

How to multiply with sum in Laravel

I would like to multiply sales.price with merchants.commission with Laravel Query Builder. Currently my query looks like this, but I unfortunately do not know what do proceed. Help is kindly appreciated.

DB::table('sales')
->leftJoin('merchants', 'sales.merchant', '=', 'merchant.id')
->sum("");

Upvotes: 1

Views: 8534

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You can actually pass a DB::raw() expression to select the sum of much more than just a column. For example:

$total = DB::table('sales')
           ->leftJoin('merchants', 'sales.merchant', '=', 'merchant.id')
           ->sum(DB::raw('sales.price * merchants.commission'));

Upvotes: 5

Related Questions