这是我的产品型号
public function orders(){
return $this->hasMany(Order::class);
}
这是我的订单模型
public function user(){
return $this->belongsTo(User::class);
}
public function product(){
return $this->belongsTo(Product::class);
}
和我的订单表结构
这是我目前的回复(我正在使用Order模型)。
{
id: 11,
quantity: 2,
customer: {
id: 6,
name: "saging",
email: "saging@gmail.com",
role_id: 3,
},
items: {
id: 7,
name: "Pellentesque semper",
description: "raesent sit amet elementum purus. ",
price: "72.21",
cover_image: "7CPVS7yjqba9iJ7J.jpg",
}
},
{
id: 12,
quantity: 2,
customer: {
id: 6,
name: "saging",
email: "saging@gmail.com",
role_id: 3,
},
items: {
id: 3,
name: "Donec nec mattis ligula",
description: "Pellentesque semper, augue at aliquet tincidunt",
price: "33.21",
cover_image: "ZJbbzjPwDXrcbkwi.jpg",
}
}
我当前使用的代码是
$products = Order::with('user','product');
return OrdersResource::collection($products->paginate(5));
并使用我的products and customer
中的方法即时列出Order Model
我想要的输出就是这样
{
id: 12,
quantity: 2,
customer: {
id: 6,
name: "saging",
email: "saging@gmail.com",
role_id: 3,
},
items: [
{
id: 3,
name: "Donec nec mattis ligula",
description: "Pellentesque semper, augue at aliquet tincidunt",
price: "33.21",
cover_image: "ZJbbzjPwDXrcbkwi.jpg",
},
{
id: 7,
name: "Pellentesque semper",
description: "raesent sit amet elementum purus. ",
price: "72.21",
cover_image: "7CPVS7yjqba9iJ7J.jpg",
}
]
}
这是我的OrderResource代码
$sub_total = $this->quantity * $this->product->price;
$discount = round((10 / 100) * $sub_total, 2);
$total = $sub_total - $discount;
return [
'id' => $this->id,
'quantity' => $this->quantity,
'address' => $this->address,
'sub_total' => $sub_total,
'discount' => $discount,
'total_price' => $total,
'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
'customer' => $this->user,
'items' => $this->product,
];
我的关系不对吗?我对laravel并不陌生,而雄辩的关系是我经常混淆的部分,因为我感到困惑。但是现在我必须面对它。
答案 0 :(得分:2)
订单和产品之间的关系存在问题,您必须设置多对多关系,为此您将需要:
创建第一个表名称order_product
应该包含order_id
和product_id
设置模型中的关系:
Product
模型:
public function orders()
{
return $this->belongsToMany('App\Order');
}
Order
模型:
public function products()
{
return $this->belongsToMany('App\Product');
}
最后,您必须创建一个ProductResource
,并且在OrderResource
中必须这样做:
return [
'id' => $this->id,
'quantity' => $this->quantity,
'address' => $this->address,
'sub_total' => $sub_total,
'discount' => $discount,
'total_price' => $total,
'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
'customer' => $this->user,
'items' => ProductResource::collection($this->products)
];
答案 1 :(得分:1)
从product_id
表中删除quantity
和orders
并创建一个表order_product
order_product(id, order_id, product_id, quantity)
订单模型
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('quantity')->withTimestamps();
}
产品型号
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('quantity')->withTimestamps();
}
现在创建一个ProductResource
类
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'price' => $this->price,
];
现在OrderResource
类中进行更改
$sub_total= 0;
$quantity = 0;
foreach($this->products as $product){
$sub_total += ($product->pivot->quantity * $product->price);
$quantity += $product->pivot->quantity;
}
$discount = round((10 / 100) * $sub_total, 2);
$total = $sub_total - $discount;
return [
'id' => $this->id,
'quantity' => $quantity,
'address' => $this->address,
'sub_total' => $sub_total,
'discount' => $discount,
'total_price' => $total,
'created_at' => Carbon::parse($this->created_at)->format('F d, Y h:i:s A'),
'customer' => $this->user,
'items' => ProductResource::collection($this->products),
];
创建订单后,插入这样的订单产品
$order->products()->attach([
10 => ['quantity' => 1],
22 => ['quantity' => 2]
]);
此处10和22是产品ID
在此处https://laravel.com/docs/5.6/eloquent-relationships#many-to-many
查看详细信息