Laravel 4级联软删除

Hal*_*zed 6 php mysql laravel laravel-4

是否有模块化方式在L4中执行级联软删除?

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

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

rob*_*lls 10

我有使用模型事件的级联删除工作,例如在我绑定到已删除事件的Product模型中,因此我可以软删除所有关系:

    // 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();
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)


Hal*_*zed 2

我确实知道这在我的模型中是可能的:

public function delete() {
  ChildTable::where('parent_id', $this->id)->delete();
  ChildTable2::where('parent_id', $this->id)->delete();
  parent::delete();
}
Run Code Online (Sandbox Code Playgroud)

但是对模型或表结构的任何更新都会导致附加/编辑......包括其他模型。