首页 > 解决方案 > Laravel SoftDelete 魔法

问题描述

我试图弄清楚为什么在Scope 和 Trait 上都声明了restoreof :SoftDeletes

软删除范围

protected function addRestore(Builder $builder)
{
    $builder->macro('restore', function (Builder $builder) {
        $builder->withTrashed();

        return $builder->update([$builder->getModel()->getDeletedAtColumn() => null]);
    });
}

特质软删除

public function restore()
{
    // If the restoring event does not return false, we will proceed with this
    // restore operation. Otherwise, we bail out so the developer will stop
    // the restore totally. We will clear the deleted timestamp and save.
    if ($this->fireModelEvent('restoring') === false) {
        return false;
    }

    $this->{$this->getDeletedAtColumn()} = null;

    // Once we have saved the model, we will fire the "restored" event so this
    // developer will do anything they need to after a restore operation is
    // totally finished. Then we will return the result of the save call.
    $this->exists = true;

    $result = $this->save();

    $this->fireModelEvent('restored', false);

    return $result;
}

第一个和后者的用途是什么?我正在尝试根据 Laravel 用法构建自己的特征/范围,但我只是没有得到这个..

如果我正确理解了我正在阅读的内容,那么两者都在做同样的事情,除了从 builder 调用的那个不会引发事件,而来自 model 的那个会。

标签: phplaravel-5

解决方案


推荐阅读