How to Implement Laravel Soft Delete to Restore or Delete Permanently
Laravel Soft Delete is a very useful option from Laravel Eloquent if you want to use it as recycle bin before permanently deleting data. In case some data gets deleted accidentally, Laravel has the option to restore the deleted data. So, you can use this feature as trash data. You can also permanently delete the data using the Laravel force delete option.
To use soft delete in your Laravel application:
While creating Laravel soft delete migration use $table->softDeletes();. This will add "deleted_at" column in the table.
In the model add use Illuminate\Database\Eloquent\SoftDeletes; and add use SoftDeletes; trait.
To Soft Delete - In the controller use delete() method on the selected row to move data to trash. It actually updates the "deleted_at" column with the timestamp.
To Restore - Use restore() method to restore soft deleted data.
To Permanently Delete - use forceDelete() method on the row selected from Trashed data.
Here, I will develop a Laravel application to show how Soft delete works. I will have list of training courses with option to move courses to trash. You can view the trashed data with option to restore it or delete permanently.
Let us create a project in Laravel. Make sure you have composer and PHP installed in your system. I have given the project name as lara_soft_delete.
This will create a folder named lara_soft_delete under the folder where you run the above command.
Step 2 - Create MySQL table and migrate
Let us create a Laravel migration for the table named 'training_courses'. This table will store the details of all courses. Go to your project folder and run below command to create model, controller and migration from VS code terminal.
Above command will create the model, controller and the migration file. I am using VS code and three files are marked below:
Update .env file for database details. I am using MySQL database named 'lara_demo', so I updated the file as below:
Migration for the custom table
We will use $table->softDeletes(); in the migration file for the table 'training_courses' along with the other columns.
So, our table will have course_title, course_descr, level (basic, Intermediate etc.) and duration column along with the column "deleted_at" for soft delete.
Let us now run the migration to create the default Laravel tables and our custom table. Run below artisan command from project root to create the tables.
This will create all the default Laravel tables as well as our custom 'training_courses' table. After migration see the structure of 'training_courses' table.
Now let us update the model for soft delete. Use SoftDelete Trait here. Below is the model:
Create Laravel Database Seeder
We will populate some data for the courses. Run below command to create a seeder file:
Update seeder file as below to insert 3 rows in training_courses table:
Update database seeder file (DatabaseSeeder.php) as below to call our seeder file:
Now run below command to create the rows:
Step 3 - Write Controller code
Controller will have few methods for each of the actions permormed on the course.
index() method to display all the courses.
moveToTrash() to move data to trash, i.e., soft delete.
showTrash() method to display all trashed data. These data can be restored or deleted permanently.
restore() method for restoring the deleted row.
forceDelete() method to delete data from trash permanently.
Let us see the code for the controller
Step 4 - Create Laravel blade views
We will create two views, one for listing of all courses and the other for viewing all deleted (trashed) data. Along with them we will have layouts and a couple of modals for user confirmations before deleting.
I have below files in the resources/views folder
header, footer and master are the layouts. view_courses.blade.php is for the listing of all applications, trashed_courses.blade.php is the list of all trashed(soft deleted) courses.
Below is the view for trashed data.
In this case there are two buttons; one for restoring and other for permanent delete. Of course there will be confirmation modal for each of the actions.
I am using some styles and the stylesheet is given below:
Step 5 - Test the application
From the project root, start the php development server:
From browser start localhost:8000. Check the Laravel soft delete, restore and permanent delete functions against the courses.