如何在 Laravel 中打印出包含多个产品的所有订单?
Posted
技术标签:
【中文标题】如何在 Laravel 中打印出包含多个产品的所有订单?【英文标题】:How to print out all orders with multiple products in Laravel? 【发布时间】:2020-10-01 17:33:16 【问题描述】:我想打印出所有包含产品的订单。我不知道如何构建 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, “is_delivery”:1, “is_online_payment”:1, “一顿饭”: [ “meal_id”:23, “数量”:1, “附加”:[ “extra_id”:10 ] , “meal_id”:24, “数量”:1, “附加”:[ “extra_id”:13 ] ]
结果 ["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.","电话":"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" :"Vxxxxxxxxxxxxxxxxxx 7.","电话":"06303900000","order_meal_id":81]
数据库 客户表:
订单表:
order_meal 表(产品)
结果应该是这样的:
["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"]]
【问题讨论】:
尝试通过将 ->unique() 应用于集合来删除重复项。喜欢$orders->unique()
你也可以看看link
它对我不起作用。一个订单应该有多个order_meal,是否可以将所有的ordermeal作为一个数组获取?
有没有和distinct
一样的方法
【参考方案1】:
我找到了解决办法! 您必须在模型中设置关系!
order.php 模型
class Order extends Model
public $table = "order";
protected $fillable = [
'customer_id',
'restaurant_id',
'is_delivery',
'delivery_price',
'coupon',
'coupon_sale',
'is_online_payment',
'total_price',
'is_paid'
];
public function ordermeal()
return $this->hasMany('App\OrderMeal','order_id','id');
public function customer()
return $this->hasOne('App\Customer','id','customer_id');
ordermeal.php 模型
class OrderMeal extends Model
public $table = "order_meal";
protected $fillable = [
'order_id',
'meal_id',
'quantity',
'price'
];
public function order()
return $this->belongsTo('App\Order','order_id','id');
public function ordermealextras()
return $this->hasMany('App\OrderMealExtras','order_meal_id','id');
public function meal()
return $this->hasOne('App\Meal','id','meal_id');
控制器
$orders = Order::where('restaurant_id', '=', $restaurantID)->orderBy('created_at', 'ASC')
->with('customer')
->with('ordermeal.meal')
->with('ordermeal.ordermealextras')->get();
return view('/pages/orders', [
'pageConfigs' => $pageConfigs, 'orders' => $orders
]);
【讨论】:
模型中的关系是正确简单的方法,你打给我贴吧以上是关于如何在 Laravel 中打印出包含多个产品的所有订单?的主要内容,如果未能解决你的问题,请参考以下文章