如何使用UpdateExistingPivot通过ID更新Pivot表?
更新订单时,我想更新特定的Order_product行。
如果我有多个产品,这些产品具有相同的订单,但具有不同的颜色和数量。
order_product
表
ID | Order_id | product_id | color | Wentity |
---|---|---|---|---|
1 | 1 1 1 | 1 1 | 1 | 1 1 |
1 1 | 1 | 1 1 | 蓝 | 1 |
我要使用update> update exexistingPivot()
,但它更新了像最后一个一样更新记录。
这就是我在更新顺序时发送的身体的样子:
{
"products": [
{
"id": 1,
"color": "red",
"quantity": 1
},
{
"id": 1,
"color": "blue",
"quantity": 3
},
{
"id": 2,
"color": "black",
"quantity": 5
}
]}
这就是我希望看到
ID | 订单product_id | product_id | 颜色 | 数量 |
---|---|---|---|---|
1 1 | 1 | 1 | 1 2 | 1 |
1 | 1 1 | 1 | 蓝色 | 3 |
3 | 1 | 2 | 黑色 | 5 |
,获得此
ID | order_id | product_id product_id | color | 量 |
---|---|---|---|---|
1 | 1 1 | 1 | 蓝色 | 3 |
2 | 1 | 1 | 蓝色 | 3 |
3 | 1 | 2 | 黑色 | 5 |
ordersController
public function update(AdminUpdateOrderRequest $request, $id)
{
$orderValidated = $request->validated();
$order = Order::findOrFail($id);
$order->update($orderValidated);
foreach ($orderValidated['products'] as $product) {
$order->products()->updateExistingPivot(
$product['id'], [
'color' => $product['color'],
'quantity' => $product['quantity'],
]
);
}
return OrderResource::make($order)->additional([
'success' => true,
]);
}
OrderModel
public function products()
{
return $this->belongsToMany(Product::class)->withPivot(['color', 'quantity', 'id'])->withTimestamps();
}
I want to update specific order_product rows when update an order.
In case I have multiple products that have the same id at the same order but with different colors and quantities.
order_product
table
id | order_id | product_id | color | quantity |
---|---|---|---|---|
1 | 1 | 1 | red | 2 |
2 | 1 | 1 | blue | 1 |
I'm trying to use updateExistingPivot()
but it updates the records like just the last one.
This is how the body I sent when updating order look like:
{
"products": [
{
"id": 1,
"color": "red",
"quantity": 1
},
{
"id": 1,
"color": "blue",
"quantity": 3
},
{
"id": 2,
"color": "black",
"quantity": 5
}
]}
That's what I expected to see
id | order_id | product_id | color | quantity |
---|---|---|---|---|
1 | 1 | 1 | red | 1 |
2 | 1 | 1 | blue | 3 |
3 | 1 | 2 | black | 5 |
Instead, got this
id | order_id | product_id | color | quantity |
---|---|---|---|---|
1 | 1 | 1 | blue | 3 |
2 | 1 | 1 | blue | 3 |
3 | 1 | 2 | black | 5 |
OrdersController
public function update(AdminUpdateOrderRequest $request, $id)
{
$orderValidated = $request->validated();
$order = Order::findOrFail($id);
$order->update($orderValidated);
foreach ($orderValidated['products'] as $product) {
$order->products()->updateExistingPivot(
$product['id'], [
'color' => $product['color'],
'quantity' => $product['quantity'],
]
);
}
return OrderResource::make($order)->additional([
'success' => true,
]);
}
OrderModel
public function products()
{
return $this->belongsToMany(Product::class)->withPivot(['color', 'quantity', 'id'])->withTimestamps();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于Manuttomany关系的枢轴表必须始终在两个模型ID上定义一个唯一的组合索引。
例如,让我们看一下Laravel的扩展生成器如何定义用户和角色之间的枢轴表:
请参阅主要定义?这样,它可以确保“ cool_id”和“ user_id”之间的任何组合都是唯一的。
在那个示例(用户,角色)中,如果我们在枢轴表中有一些额外的colums,我们可以使用updateExistingPivot()两种方式:
A pivot table for a manyToMany relationship must always define a unique combined index on both models ids.
Let's look for example at how Laravel's extended generator defines a pivot table between User and Role:
See the primary definition ? That way it ensures that any combination between 'role_id' and 'user_id' is unique.
in that example (User, Role) and if we supposedly had some extra colums in our pivot table, we could use updateExistingPivot() either ways: