php – Laravel 4 Cascading Soft Deletes

前端之家收集整理的这篇文章主要介绍了php – Laravel 4 Cascading Soft Deletes前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有模块化方式在L4中执行级联软删除

我的数据库已经设计为使用硬删除来执行此操作,因为所有表都与另一个表相关..但是,我正在使用软删除,并且实际上不希望在我的模型中重载delete()方法 – 仅仅是因为( A)模型的数量,以及(B)当其他模型改变时必须在所有模型中编辑delete()方法.

任何指针或提示将不胜感激.

我有使用 model events的级联删除工作,例如在产品模型中我绑定到已删除的事件,因此我可以软删除所有关系:
// 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相关文章