I want to export some results in Excel from the Model method, so I added this,
public static function getAllData()
{
$records = DB::table('orders as t1')
->leftjoin('payments as t2', 't1.ord_id', '=', 't2.pay_ord_id')
->select(DB::raw("case when pay_type_id=190 then t1.ord_total - t2.pay_amount as Paid_Amount_Online"),
...
->get();
return $records;
}
So as you can see I want to check if the payment type id is equals to 190 that means user has paid it with wallet and the remaining balance is paid online.
So in order to get the Paid_Amount_Online value, I need to subtract the whole order total amount from the payment amount:
case when pay_type_id=190 then t1.ord_total - t2.pay_amount as Paid_Amount_Online
But this obviously wrong and returns syntax error.
So I wonder how can I subtract the amount values from eachother when using sql case expression.
Your
is raw SQL injected into the query. The syntax is like this:
That is:
casewhen, followed by an expression, continuing with athenand ending with the actual valueelsethat stands for a fallback logic when all conditions are falseendIn your code you have a
case, a condition with awhenand athen, but you are missing itsend.