php - Laravel SoftDelete 魔法
问题描述
我试图弄清楚为什么在Scope 和 Trait 上都声明了restore
of :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 的那个会。
解决方案
推荐阅读
- javascript - 附加选项以选择不起作用。CSS对此负责吗?
- c++ - 在c ++中调用类指针的默认构造函数作为map的值
- laravel - 使用关系从 Laravel 中的第二个表中获取最小值
- python-3.x - 如何在 bs4 中对 find_all() 的标记进行部分调节?
- python - 在 OpenCV 中随图像移动的增强现实线
- r - 两组二进制变量的条形图?
- android - TextDecoration 不适用于 LazyColumn 项目
- sqlite - sqlite 的意外行为
- python - ValueError:找不到与“+xxxxxxxxxxx”对应的任何实体
- javascript - 新的 Float32Array 耗时异常的原因是什么?