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

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 ModelsSometimes 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();