我在mysql数据库中有一个名为transactions的表,我将使用Laravel的雄辩ORM(Laravel 5)来得到乘法列(jumlah)和(harga)的和,我的表看起来很像:

+----+----------+-------------+--------+---------+------------+---------------------+---------------------+
| id | order_id | nama_barang | jumlah | harga   | keterangan | created_at          | updated_at          |
+----+----------+-------------+--------+---------+------------+---------------------+---------------------+
|  4 |        3 | PS 4        |      2 | 4500000 | Hitam      | 2015-05-20 08:55:26 | 2015-05-20 08:56:14 |
+----+----------+-------------+--------+---------+------------+---------------------+---------------------+

我想得到这样的结果:
select sum(jumlah * harga) from transactions;
+---------------------+
| sum(jumlah * harga) |
+---------------------+
|             9000000 |
+---------------------+

如何使用ORM Laravel,我试过这个,但它给了我0
$amount = Transaction::all()->sum('(jumlah * harga)');

最佳答案

试着用这个

$amount = Transaction::select(DB::raw('sum(jumlah * harga) as total'))->get();

09-26 11:46