Laravel:使用弹拨从数据库中获取单个值


Laravel : Getting Single value from the database using pluck

大家好,我正在使用Laravel,我正在尝试使用pluck从数据库中获取单个值,问题是查询必须以Laravel的方式编写,而不是以正常的方式编写 sql.so 如何编写此查询Laravel中的sql查询?我希望能够使用 AS 更改列名并SUM .我实际上想要的只是从total_quantity列中获取值,以执行此操作,我必须使用 pluck。知道如何编写该查询吗?

提前谢谢。

$PhysicalStock=DB::select("select sum(quantity) as `total_quantity` from InOutProducts where productID=$productID and InAndOut=1;")->pluck('total_quantity');

阅读更多关于 Larvel DB 和 Eloquent 的信息。

DB::table('InOutProducts')->selectRaw('SUM(quantity) as total_quantity')->where('productId', '=', $productID)->where('InAndOut', '=', 1)->pluck('total_quantity');