1

How can I build the following MySQL query using Laravel-5?

select orders.*, oi.items from orders 
inner join (select order_id, group_concat(item_name SEPARATOR ', ') as items 
from orders_items group by order_id) as oi on oi.order_id = orders.id;

EDIT

The suggested link did not resolve my problem, however I did find a solution and will post it below for others.

Community
  • 1
  • 1
V4n1ll4
  • 5,973
  • 14
  • 53
  • 92

1 Answers1

2
$order  = $order->select('orders.*', 'oi.items');
$order->join(DB::raw('(select order_id, group_concat(item_name SEPARATOR ",") as items from orders_items group by order_id) as oi'), function($join)
{
    $join->on('oi.order_id', '=', 'orders.id');
});
$order->whereRaw('find_in_set ("'.$value.'", oi.items)');

$rows = $order->get();
V4n1ll4
  • 5,973
  • 14
  • 53
  • 92