Laravel Soft Delete – How to safely delete data in Laravel

Tram Ho

Introducing softDeletes in Laravel

Normally, querying data in database is very basic, operations such as reading data, organizing data, deleting data and adding new data integrated in laravel are also quite complete. Particularly in deleting data, there are two schools: delete and hide in each query. So what are the advantages and disadvantages of these 2 ways? Let’s go to each section to understand better.

1. Hard delete

  • Hard deletion is very normal, ie deleting a record in the table, and will not be restored (unless the database backup).
  • This has the benefit of freeing up storage space, but there is a huge disadvantage that is irreversible. So later on people were very limited to using hard delete, but instead switched to hide mechanism instead of hard delete.
  • Hard delete command:

2. Soft delete

Unlike hard erasing, soft deletion simply hides a record in a query, for example the users table we have:

idnamephoneis_disabled
firstJone Doe+8499898998670
2Maria Zoo+8499898998880
3Marina Zoo+8499898998660
  • The hard delete statement has id 1:

=> After running this command, the data of users is only:

idnamephoneis_disabled
2Maria Zoo+8499898998880
3Marina Zoo+8499898998660
  • So if soft removal is applied then:
  • The mechanism of operation of the soft delete is to rely on a column to hide the current record in the query, for example using soft delete to delete the record with id 1
  • The command to soften record with id 1 is:

=> After running this command, the data of users will be:

idnamephoneis_disabled
firstJone Doe+849989899867first
2Maria Zoo+8499898998880
3Marina Zoo+8499898998660
  • Table users with id 1 have not been deleted but only updated the column is_disabled = 1. So how can we prove that this record has been deleted: Very simply, when we select the data, we only need to add the condition is_disabled = 0 is okay, simple right.

=> After running this command, the data of users is only:

idnamephoneis_disabled
2Maria Zoo+8499898998880
3Marina Zoo+8499898998660

Very useful is not it, the record is still there but we do not select it only, that is the mechanism that softDeletes in laravel works.

Laravel softDeletes

  • The use of softDeletes in laravel is much more clear than the usual way above, the mechanism is the same but laravel has supported so we can select normally without using where is_disabled = 0 . If select always where to add this condition is a little annoying, isn’t it? Let’s configure softDeletes in laravel.

1. Install

  • Create project laravel with the name soft_delete_laravel:

composer create-project --prefer-dist laravel/laravel soft_delete_laravel

  • In app / Providers / AppServiceProvider.php file we add the line:

in the User model we add

  • In migration create the table structure we add:

  • Line $table->softDeletes(); will create deleted_at column with default value of null (similar to column is_disabled with default value as 0)
  • Then we run the command: php artisan migrate

2. Using Soft Deletes:

  • Delete record: AppUser::where('id', 1)->delete(); => When we call delete method for users with id equal to 1, in this record will update deleted_at column with a value other than null (when deleted, it will update time)
  • Get data: AppUser::all(); will retrieve all records in the users table, excluding records with deleted_at other than null. Very clear, right.
  • to retrieve all records including deleted records by softDeletes: AppUser::withTrashed()->get();
  • To retrieve only the deleted records: AppUser::onlyTrashed()->get();
  • To retrieve deleted records with softDeletes: AppUser::withTrashed()->where('id', 1)->restore();
  • Delete the record permanently: AppUser::withTrashed()->where('id', 1)->forceDelete();

summary

  • Through this we see that applying softDeletes will be very safe for restoring deleted data. How to apply to database design is also very simple, if using laravel framework, it is already supported to the teeth. A little bit of contribution to make us more and more progressive, we hope you all share together to make us more and more develop.
Share the news now

Source : Viblo