Laravel 4级联软删除 [英] Laravel 4 Cascading Soft Deletes
问题描述
在L4中是否有模块化的方法来执行级联的软删除?
Is there a modular way to perform cascading soft deletes in L4?
我的数据库已经被设计为使用硬删除来做到这一点,因为所有表都与另一个表相关..但是,我使用的是软删除,并且真的不想在模型中重载delete()
方法-简单由于(A)模型的数量,以及(B)当其他模型改变时,必须在所有模型中编辑delete()
方法.
My database is already designed to do this with hard deletes because all tables are related to another.. however, I'm using soft deletes and really do not want to have to overload the delete()
method in my models - simply due to (A) the amount of models, and (B) having to edit the delete()
method in all models when other models change.
任何指针或技巧都将不胜感激.
Any pointers or tips would be appreciated.
推荐答案
I've got cascading deletes working using model events, for example in a Product model I bind to the deleted event so I can soft-delete all relations:
// Laravel's equivalent to calling the constructor on a model
public static function boot()
{
// make the parent (Eloquent) boot method run
parent::boot();
// cause a soft delete of a product to cascade to children so they are also soft deleted
static::deleted(function($product)
{
$product->images()->delete();
$product->descriptions()->delete();
foreach($product->variants as $variant)
{
$variant->options()->delete();
$variant->delete();
}
});
}
这篇关于Laravel 4级联软删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!