Wednesday, March 8, 2017

How do I use raw select in Eloquent?

My SQL is something like this:

SELECT
  NAME,
  PLACE,
  ETC,
  format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE
FROM
  THE_TABLE

And my Eloquent query is:

theTable::select(
    'NAME',
    'PLACE',
    'ETC',
    'format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE'
)
->get();

But it doesn't work because Eloquent interprets it something like select name, place, etc, format sum from the_table.

I have treid to use DB::raw but then it says that it didn't expect object.

theTable::select(
    'NAME',
    'PLACE',
    'ETC',
    DB::raw('format(((sum((AMOUNT * U_PRICE)) - ((sum((AMOUNT * U_PRICE)) * 20) / 100)) * 1.18),2) AS TOTAL_PRICE')
)
->get();

So, how can I use raw SELECT statement in Eloquent?



via ilhan

Advertisement