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.
4
Answers
check :
before as use
end
Your
is raw SQL injected into the query. The syntax is like this:
That is:
case
when
, followed by an expression, continuing with athen
and ending with the actual valueelse
that stands for a fallback logic when all conditions are falseend
In your code you have a
case
, a condition with awhen
and athen
, but you are missing itsend
.You just forgot END at the end of your case expression: