I want to print out all the orders containing the products. I can not figure out how to build the sql query to return every order once. As you can see it returns the order with id 4 twice because it has 2 products in it.
Controller
$orders = DB::table('order')
->join('customer', 'order.customer_id', '=', 'customer.id')
->leftjoin('order_meal', 'order.id', '=', 'order_meal.order_id')
->select('order.*', 'customer.firstname', 'customer.lastname', 'customer.country', 'customer.city', 'customer.zipcode', 'customer.address', 'customer.phone', 'order_meal.id AS order_meal_id')
->where('order.restaurant_id', '=', $restaurantID)
->orderBy('order.created_at', 'ASC')
->get();
if ($orders === null) {
return redirect('/');
}
return view('/pages/orders', [
'pageConfigs' => $pageConfigs, 'orders' => $orders
]);
View: {{ $orders }}
The inserted data as JSON
{ "restaurant_id": 1, "customer_id": 1, "is_delivery": 1, "is_online_payment": 1, "meal": [ { "meal_id": 23, "quantity": 1, "extras": [ { "extra_id": 10 } ] }, { "meal_id": 24, "quantity": 1, "extras": [ { "extra_id": 13 } ] } ] }
Result [{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","firstname":"Dominik","lastname":"Balogh","country":"Magyarorsz\u00e1g","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","phone":"06303900000","order_meal_id":80},
{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","firstname":"Dominik","lastname":"Balogh","country":"Magyarorsz\u00e1g","city":"Szentendre","zipcode":2000,"address":"Vxxxxxxxxxxxxxxxx 7.","phone":"06303900000","order_meal_id":81}]
The result shoul look like this:
[{"id":4,"customer_id":1,"restaurant_id":1,"is_delivery":1,"delivery_price":0,"coupon":null,"coupon_sale":"0","is_online_payment":1,"total_price":0,"is_paid":0,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05","order_meal":[{"id":80,"order_id":4,"meal_id":23,"quantity":1,"price":1850,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05"},{"id":81,"order_id":4,"meal_id":24,"quantity":1,"price":1890,"created_at":"2020-06-11 22:15:05","updated_at":"2020-06-11 22:15:05"}]}]