我想把所有的订单都打印出来。我不知道如何构建sql查询来返回每个订单一次。正如您所看到的,它返回id为4的订单两次,因为其中有2个产品。
控制器
$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
]);
查看:{{$orders}}
作为json插入的数据
{“餐厅id”:1,“顾客id”:1,“是送货”:1,“是在线付款”:1,“用餐”:[{“用餐id”:23,“数量”:1,“额外服务”:[{“额外服务id”:10}]},{“用餐id”:24,“数量”:1,“额外服务”:[{“额外服务id”:13}]}
结果[{“id”:4,“customer\u id”:1,“restaurant\u id”:1,“is\u delivery”:1,“delivery\u price”:0,“coupon”:null,“coupon\u sale”:“0”,“is\u online\u payment”:1,“total\u price”:0,“is\u payment”:0,“created\u at”:“2020-06-11 22:15:05”,“updated\u at”:“2020-06-11 22:15:05”,“firstname”:“dominik”,“lastname”:“balogh”,“country”:“magyarorsz\u00e1g”,“city”:“szentendre”,“zipcode”:2000,“address”:“vxx7.”,“phone”:“06303900000”,“order\u mean\u id”:80},
{“id”:4,“customer\u id”:1,“restaurant\u id”:1,“is\u delivery”:1,“delivery\u price”:0,“coupon”:null,“coupon\u sale”:“0”,“is\u online\u payment”:1,“total\u price”:0,“is\u payment”:0,“created\u at”:“2020-06-11 22:15:05”,“updated\u at”:“2020-06-11 22:15:05”,“firstname”:“dominik”,“lastname”:“balogh”,“country”:“magyarorsz\u00e1g”,“city”:“szendre”,“zipcode”:2000,“address”:“vxx7.”,“phone”:“06303900000”,“order\u mean\u id”:81}]
数据库客户表:
订单表:
订购餐台(产品)
结果应该是这样的:
[{"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"}]}]
1条答案
按热度按时间tsm1rwdh1#
我找到了解决办法!你必须在模型中设置关系!
order.php模型
ordermein.php模型
控制器