我有两个模型,它们由一个具有复合键的关系连接 - 这些是产品和类别.我需要在所有表上使用软删除,以便在需要时可以恢复模型和关系.
在我的产品型号中,我有:
function categories()
{
return $this->belongsToMany('App\Category', 'product_categories')->whereNull('product_categories.deleted_at')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
在我的分类模型中,我有:
function products()
{
return $this->belongsToMany('App\Product', 'product_categories')->whereNull('product_categories.deleted_at')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
我在其他地方读到了关于链接whereNull方法的问题,因为查询$category->products->contains($product->id)会以其他方式返回软删除的关系.
我的问题是处理删除和恢复这些软删除关系的最佳方法是什么?例如,为了恢复,我试过:
$product->categories()->restore($category_id);
Run Code Online (Sandbox Code Playgroud)
上面产生了一个SQL错误,说出deleted_at字段是不明确的(因为它将类别表加入了product_categories).
更新 - 根本问题似乎是BelongsToMany类不支持软删除 - 因此附加,分离和同步都执行硬删除.覆盖这个课程的最佳方法是什么?
我有两个模型,Product和Attribute。在“产品”和“属性”之间有一个“ product_attributes”联接表。在两个模型中都使用belongsToMany()定义了该关系,并且表的键是product_id和attribute_id的复合之一。
我可以成功检索记录并将其存储在联接表中-因此,一切都按预期运行,但未设置product_attributes中的created_at和updated_at。
以上是设计使然,我可以做些什么来纠正此问题?