简体   繁体   中英

How to restore a soft deleted record using Laravel's Query builder?

我一直在为我的项目使用查询构建器,并希望使用查询构建器恢复软删除的记录。

Look here 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:

$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.

So to restore a soft deleted record all you have to do is set the deleted_at column to null.

As you wanted to do it using query builder

DB::table('table')
  ->where('id', $recordToRestoreId)
  ->update(['deleted_at' => null]);

If using Eloquent

Model::withTrashed()
     ->where('id', $recordToRestoreId)
     ->restore();

or if you have a model instance

$model->restore();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM