簡體   English   中英

如何在Laravel Eloquent中更新(HasMany Relationship)

[英]How to update in Laravel Eloquent (HasMany Relationship)

我有兩個模塊,分別是 Product 和 Items Modules 這兩個模塊之間的關系是:Product hasMany items 和 Items belongsTo product

Product Module 的表是:id, product_name Items Module 的表是:id, products_id, item_name, quantity, price

這是我的更新代碼

public function update(Request $request)
{
    $product = Product::findOrFail($id);
    $product->product_name = $request->product_name;
    $product->update();
    // Remove all items and add new
    $product->items()->delete();
    // Save each items
    if($request->get('item_name')){
        foreach($request->get('item_name') as $key => $item_name)
        {   
            $items = new Items();
            $items->products_id = $product->id;
            $items->item_name = $item_name;
            $items->quantity = $request->quantity[$key];
            $items->price = $request->price[$key];
            $items->save();
        };
    };
}

此更新代碼的結構是每當有更新時,將刪除每個項目並將更新信息添加為新行。

我想要的是項目不應該被刪除並添加為新項目。 這是因為我想維護項目 ID。 我應該修改我當前的代碼什么?

如果您的目的是使其更加動態地添加刪除更新項目,我們必須用另一種方式來處理這個問題!
不幸的是 Laravel 沒有提供像sync()這樣的方法來處理多對一的關系,只有多對多 所以你必須在你的代碼中插入一些額外的邏輯。 我們將使用id鍵來處理這個邏輯。
因此,對於這項工作,來自請求的所有產品項目都必須具有密鑰id 新項目(數據庫中尚不存在的項目)的id鍵必須為空。

例如:

[
  {
    "id": 1,
    "name": "Item 1",
    "quantity": 2,
    "price": 10,
    "product_id": 1,
  },
  {
    "id": "",
    "name": "Item 2",
    "quantity": 3,
    "price": 22,
    "product_id": 1,
  },
  {
    "id": "",
    "name": "Item 3",
    "quantity": 9,
    "price": 33,
    "product_id": 1,
  }
]

請注意,只有第一項將被更新,第二項和第三項將被添加,因為id鍵為空,並且不進入數組但存在於數據庫中的項目將被刪除。

所以,我們走吧!

1 - 為有 id項目、沒有 id項目和有 id 的項目創建 3 個輔助變量。 請注意,我們使用collect()方法來幫助我們處理 arrays:

$items_without_id = collect($items)->where('id', '');
$items_with_id = (clone collect($items))->where('id', '!=', '');
$items_ids = $items_with_id->pluck('id');

2 - 使用 id 更新項目:

foreach ($items_with_id as $item) {
   $obj = App\ProductItem::find($item['id']);
   $obj->name = $item['name'];
   $obj->price = $item['price'];
   $obj->quantity = $item['quantity'];
   $obj->save();
}

3 - 刪除未進入數組的項目,我們假設必須刪除這些項目:

$product->items()->whereNotIn('id', $items_ids)->delete();

4 - 最后插入沒有 id 的項目。 我們假設必須添加這些項目:

$items_without_id->each(function ($item) use ($product) {
   $obj = new App\ProductItem();
   $obj->name = $item['name'];
   $obj->price = $item['price'];
   $obj->quantity = $item['quantity'];
   $obj->product_id = $product->id;
   $obj->save();
});

完整代碼:

$product = App\Product::findOrFail($id);
$items = $request->items;

$items_without_id = collect($items)->where('id', '');
$items_with_id = (clone collect($items))->where('id', '!=', '');
$items_ids = $items_with_id->pluck('id');

foreach ($items_with_id as $item) {
   $obj = App\ProductItem::find($item['id']);
   $obj->name = $item['name'];
   $obj->price = $item['price'];
   $obj->quantity = $item['quantity'];
   $obj->save();
}

$product->items()->whereNotIn('id', $items_ids)->delete();

$items_without_id->each(function ($item) use ($product) {
   $obj = new App\ProductItem();
   $obj->name = $item['name'];
   $obj->price = $item['price'];
   $obj->quantity = $item['quantity'];
   $obj->product_id = $product->id;
   $obj->save();
});

使用 postman 進行測試:

在此處輸入圖像描述

希望這可以幫助。 對不起,我的英語不好。

我有兩個模塊,分別是 Product 和 Items Modules 這兩個模塊之間的關系是:Product hasMany items 和 Items ownTo product

產品模塊的表是:id,product_name 項目模塊的表是:id,products_id,item_name,數量,價格

這是我的更新代碼

public function update(Request $request)
{
    $product = Product::findOrFail($id);
    $product->product_name = $request->product_name;
    $product->update();
    // Remove all items and add new
    $product->items()->delete();
    // Save each items
    if($request->get('item_name')){
        foreach($request->get('item_name') as $key => $item_name)
        {   
            $items = new Items();
            $items->products_id = $product->id;
            $items->item_name = $item_name;
            $items->quantity = $request->quantity[$key];
            $items->price = $request->price[$key];
            $items->save();
        };
    };
}

此 UPDATE 代碼的結構是每當有更新時,將刪除每個項目並將更新信息添加為新行。

我想要的是這些項目不應該被刪除並添加為新的。 這是因為我想維護項目 ID。 我應該對當前代碼進行哪些修改?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM