Irwan Sunario Irwan Sunario - 20 days ago 11
jQuery Question

how to query builder select inside select on laravel

I have some trouble with Laravel Query Builder. I want to convert this SQL Query:

select (select sum(amount) from transactions t
join sale w on w.id = t.sale_id
join types s on s.id = w.type_id
where s.name = 'Bronze'
) as sum_amount
from transactions
limit 1;


How to implement this query on my laravel query builder ?
I'm using laravel 5.2

hope someone help me :) thanks

Answer

You can try this:

DB::table('transactions')
  ->select(DB::raw("(select sum(amount) from transactions t
      join sale w on w.id = t.sale_id
      join types s on s.id = w.type_id
      where s.name = 'Bronze'
  ) as sum_amount"))
  ->take(1)
  ->get();

Hope this helps

Comments