Laravel SoftDelete魔法

时间:2018-04-26 09:21:06

标签: php laravel-5

我想弄清楚为什么restore SoftDeletes在范围和特征上被宣布:

SoftDeletingScope

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

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

特质SoftDeletes

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的用法建立我自己的特征/范围,但我只是没有得到这个...

如果我理解我正在阅读的内容,两者都在做同样的事情,除了从构建器调用的那个不会引发事件,而模型中的那个引发事件。

0 个答案:

没有答案