所以我創建了兩個模型:“類別”和“產品”,在這兩個模型中我添加了功能:
public function product()
{
return $this->belongstoMany(Product::class);
}
和
public function category()
{
return $this->belongstoMany(Category::class);
}
現在我在資料庫中: category_id 和 product_id
當我創建一個像這樣的關系時: category_id 1 和 product_id 2
如何在不洗掉所有輸入的情況下洗掉它。
到目前為止我的功能:
$category = Category::where($request->categoryId);
$category->product()->detach();
但這實際上會洗掉或分離 categoryId 的所有條目。
我現在該怎么做?
uj5u.com熱心網友回復:
始終先瀏覽檔案=)
// IDs to be detached
$productIds = [ 1, 2, 3 ];
$category->product()->detach($productIds);
// Or single record
$category->product()->detach(2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/407028.html
標籤:
上一篇:獲得暢銷課程
