我怎样才能得到我减去的 2 行的总数?
How can i get the total of the 2 rows that i subtract?
public function vparticulardiscountamount($id)
{
$fetch = DB::table('tbl_payment_user_map')
->leftJoin('tbl_payment_particular', 'tbl_payment_user_map.payment_particular', '=',
'tbl_payment_particular.id')
->leftJoin('tbl_discount', 'tbl_payment_user_map.discount', '=', 'tbl_discount.id')
->select('tbl_payment_particular.amount','tbl_discount.amount as
discountamount',DB::raw('(tbl_payment_particular.amount - tbl_discount.amount) as
total'))
->where('tbl_payment_user_map.payment_user',$id)
->get();
return response()->json(['results' => $fetch], 200);
}
您可以使用 Collection
的 sum
方法并将结果添加到您的响应中。
return response()
->json(['results' => $fetch, 'sum' => $fetch->sum('total')], 200);
或
return response()
->json(['results' => ['data' => $fetch, 'sum' => $fetch->sum('total')]], 200);
public function vparticulardiscountamount($id)
{
$fetch = DB::table('tbl_payment_user_map')
->leftJoin('tbl_payment_particular', 'tbl_payment_user_map.payment_particular', '=',
'tbl_payment_particular.id')
->leftJoin('tbl_discount', 'tbl_payment_user_map.discount', '=', 'tbl_discount.id')
->select('tbl_payment_particular.amount','tbl_discount.amount as
discountamount',DB::raw('(tbl_payment_particular.amount - tbl_discount.amount) as
total'))
->where('tbl_payment_user_map.payment_user',$id)
->get();
return response()->json(['results' => $fetch], 200);
}
您可以使用 Collection
的 sum
方法并将结果添加到您的响应中。
return response()
->json(['results' => $fetch, 'sum' => $fetch->sum('total')], 200);
或
return response()
->json(['results' => ['data' => $fetch, 'sum' => $fetch->sum('total')]], 200);