[英]How to restore a soft deleted record using Laravel's Query builder?
我一直在为我的项目使用查询构建器,并希望使用查询构建器恢复软删除的记录。
Look here https://laravel.com/docs/5.6/eloquent#soft-deleting 请看这里https://laravel.com/docs/5.6/eloquent#soft-deleting
Restoring Soft Deleted Models Sometimes you may wish to "un-delete" a soft deleted model. 恢复软删除的模型有时您可能希望“取消删除”软删除的模型。 To restore a soft deleted model into an active state, use the restore method on a model instance:
要将软删除的模型还原为活动状态,请在模型实例上使用restore方法:
$flight->restore();
or 要么
Model::query()->restore();
IF you want to do it manually.. just 如果你想手动做..只是
Model::whereNotNull('deleted_at')->update([
'deleted_at' => null
]);
What soft deleting does is setting a value to deleted_at
column and then filter records where the deleted_at
column has a value using a global scope. 软删除的作用是将值设置为
deleted_at
列,然后使用全局范围过滤deleted_at
列具有值的记录。
So to restore a soft deleted record all you have to do is set the deleted_at
column to null. 因此,要恢复软删除记录,您只需将
deleted_at
列设置为null即可。
As you wanted to do it using query builder 正如您想要使用查询构建器一样
DB::table('table')
->where('id', $recordToRestoreId)
->update(['deleted_at' => null]);
If using Eloquent 如果使用Eloquent
Model::withTrashed()
->where('id', $recordToRestoreId)
->restore();
or if you have a model instance 或者如果你有一个模型实例
$model->restore();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.