Laravel 自定义软删除还原无法正常工作
Laravel custom soft delete restore not working properly
向 Larave 软删除功能添加新列deleted_flag
小整数。
trait CustomSoftDeleteTrait
{
use SoftDeletes;
protected function runSoftDelete()
{
$query = $this->setKeysForSaveQuery($this->newModelQuery());
$time = $this->freshTimestamp();
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$columns[$this->getDeletedFlagColumn()] = 1; //<-- here is the deleting
$query->update($columns);
}
protected function restore()
{
if ($this->fireModelEvent('restoring') === false) {
return false;
}
$this->{$this->getDeletedFlagColumn()} = 0; //<-- here is restoring
$this->{$this->getDeletedAtColumn()} = null;
$this->exists = true;
$result = $this->save();
$this->fireModelEvent('restored', false);
return $result;
}
public function getDeletedFlagColumn()
{
return defined('static::DELETED_FLAG') ? static::DELETED_FLAG : 'deleted_flag';
}
}
模型迁移,
Schema::create('families', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('family_type');
$table->timestamp('create_date')->nullable();
$table->timestamp('update_date')->nullable();
$table->timestamp('delete_date')->nullable();
$table->tinyInteger('delete_flg')->nullable();
});
在模型中使用自定义特征,
class Family extends Model
{
use CustomSoftDeleteTrait;
protected $guarded = [
'id',
];
const CREATED_AT = 'create_date';
const UPDATED_AT = 'update_date';
const DELETED_AT = 'delete_date';
const DELETED_FLAG = 'delete_flg';
}
当使用 $family->delete()
删除模型时,delete_date
和 delete_flg
列都被设置。恢复模型时,只有一个字段 delete_date
设置为空。 delete_flg
字段保持不变为 1。
这是一个非常值得发现的 'tricky' 错误。
但是您所要做的就是在自定义特征中将 restore() 函数的修饰符从 protected 更改为 public。
protected function restore() { /**/ }
应该变成
public function restore() { /**/ }
无法从您的家庭模型访问受保护的功能。所以 PHP 简单地使用 Eloquent 软删除特征中的原始 restore() 方法。
向 Larave 软删除功能添加新列deleted_flag
小整数。
trait CustomSoftDeleteTrait
{
use SoftDeletes;
protected function runSoftDelete()
{
$query = $this->setKeysForSaveQuery($this->newModelQuery());
$time = $this->freshTimestamp();
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$columns[$this->getDeletedFlagColumn()] = 1; //<-- here is the deleting
$query->update($columns);
}
protected function restore()
{
if ($this->fireModelEvent('restoring') === false) {
return false;
}
$this->{$this->getDeletedFlagColumn()} = 0; //<-- here is restoring
$this->{$this->getDeletedAtColumn()} = null;
$this->exists = true;
$result = $this->save();
$this->fireModelEvent('restored', false);
return $result;
}
public function getDeletedFlagColumn()
{
return defined('static::DELETED_FLAG') ? static::DELETED_FLAG : 'deleted_flag';
}
}
模型迁移,
Schema::create('families', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('family_type');
$table->timestamp('create_date')->nullable();
$table->timestamp('update_date')->nullable();
$table->timestamp('delete_date')->nullable();
$table->tinyInteger('delete_flg')->nullable();
});
在模型中使用自定义特征,
class Family extends Model
{
use CustomSoftDeleteTrait;
protected $guarded = [
'id',
];
const CREATED_AT = 'create_date';
const UPDATED_AT = 'update_date';
const DELETED_AT = 'delete_date';
const DELETED_FLAG = 'delete_flg';
}
当使用 $family->delete()
删除模型时,delete_date
和 delete_flg
列都被设置。恢复模型时,只有一个字段 delete_date
设置为空。 delete_flg
字段保持不变为 1。
这是一个非常值得发现的 'tricky' 错误。
但是您所要做的就是在自定义特征中将 restore() 函数的修饰符从 protected 更改为 public。
protected function restore() { /**/ }
应该变成
public function restore() { /**/ }
无法从您的家庭模型访问受保护的功能。所以 PHP 简单地使用 Eloquent 软删除特征中的原始 restore() 方法。