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:

  1. While creating Laravel soft delete migration use $table->softDeletes();. This will add "deleted_at" column in the table.
  2. In the model add use Illuminate\Database\Eloquent\SoftDeletes; and add use SoftDeletes; trait.
  3. 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.
  4. To Restore - Use restore() method to restore soft deleted data.
  5. 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.

Step1 - Create a Laravel project

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:

Laravel soft delete

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.

Laravel soft delete table structure

Now let us update the model for soft delete. Use SoftDelete Trait here. Below is the model:

app/Models/TrainingCourse.php

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.

  1. index() method to display all the courses.
  2. moveToTrash() to move data to trash, i.e., soft delete.
  3. showTrash() method to display all trashed data. These data can be restored or deleted permanently.
  4. restore() method for restoring the deleted row.
  5. forceDelete() method to delete data from trash permanently.

Let us see the code for the controller

app/Http/Controllers/TrainingCourseController.php

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

Laravel Soft delete views

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.

resources/views/layouts/header.blade.php

resources/views/layouts/footer.blade.php

resources/views/layouts/master.blade.php

resources/views/view_courses.blade.php

In above code, we have a simple list of all courses with a delete button (an icon for trash) against each course. A modal for confirmation will open when clicked. Also a javascript function is called just to write the id of course in a form in the modal. We will see it in the modal.

Below is the view for trashed data.

resources/views/trashed_courses.blade.php

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.

Routes

I am using some styles and the stylesheet is given below:

public/css/style.css

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.

Download Source Code from github.

Watch YouTube Video