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);
}

Solution 1:

You could use the Collection's sum method and add the result to your response.

return response()
    ->json(['results' => $fetch, 'sum' => $fetch->sum('total')], 200);

or

return response()
    ->json(['results' => ['data' => $fetch, 'sum' => $fetch->sum('total')]], 200);