简体   繁体   English

如何使用Laravel的“查询”构建器还原软删除的记录?

[英]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.

相关问题 如何在Laravel中编辑软删除的数据而不将其还原? - How to edit soft deleted data without restore it in Laravel? 无法在Laravel中恢复软删除的数据 - Not able to restore soft deleted data in laravel Laravel恢复已删除的记录 - Laravel restore back deleted record 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? 使用 Laravel 7 的查询生成器的软删除 onlyTrashed 方法时出错 - error using soft delete onlyTrashed method with the query builder of Laravel 7 如何在Laravel 4中查询和过滤软删除的口才模型 - How to query and filter soft-deleted Eloquent models in Laravel 4 是否有任何 function 从存储库设计模式恢复软删除记录? - Is there any function that restore soft deleted record from repository design pattern? Laravel使用查询生成器更新记录 - Laravel Update Record using Query Builder 如何使用 Laravel 的 Query Builder 在 JOIN 查询中生成 USING 子句? - How to generate a USING clause in JOIN queries with Laravel's Query Builder? 在 Laravel 的查询生成器中使用变量(for 循环) - Using variable (for loop) in Laravel's Query Builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM