In this tutorial, we will learn about laravel soft delete feature which allows us to delete the records from the database temporarily not permanantily.
Suppose you are developing laravel CRUD application system and you want the feature that will delete the records from the frontend it will only hide from the frontend not permanently delete them from the database.
So in this situation, you will implement the laravel soft delete feature. The benefit of this feature is that when someone deletes the records from the frontend (even if accidental) then you can restore those records from the database.
So let’s get started to learn about the laravel soft delete feature with the help of an example.
Step 1: Laravel Installation
Let’s first create a new project and install a laravel. To install the laravel, run the following command in your terminal.
If you already have laravel installed on your system then you can skip this step. You can also check our full detailed guide on how to install laravel and composer?
Step 2: Database Configuration
In this step, we have to configure the database. If you have fresh install the laravel project then you have to configure database settings in the .env file.
So open the .env file in your root directory of the project and then update the database details in it. See the following laravel 9 database configuration code.
Note: Don’t forget to replace the database credential with your database details and then save the file.
Step 3: Create Migration
Now we will create migration to set up the fields of the database. Laravel migrations are the files of the database schema and you can add the fields in this file for the database table columns.
Run the following command to create a migration file for the posts table.
The above command will create the migration file for our laravel soft delete example project. You can see this file in database/migrations directory. So open this file and add the table fields that are required in your case.
As you can see in the above code we have added the
$table->softDeletes() field in the posts table schema. The
softDeletes() method will implement the laravel soft delete feature and create a “deleted_at” column in the table.
After making all the changes let’s migrate this file to create a posts table in the database. Run the following command to migrate.
Step 4: Create Model
Now we will create a model for our laravel soft delete example project. And in this model, we will use the softDeletes trait. To create a model in laravel, run the following command.
The above command will create a model file in the app/Models directory. In this model class, we have to use the softDeletes trait, see the following code for reference.
Note: Make sure you have all the schema fields in the
$fillable array and for the soft delete field “deleted_at” should be a separate array field of the
Step 5: Create Controller
To implement the laravel soft delete functionality, we have to create a controller that will take action on our database table records. Run the following command to create a controller in laravel.
It will create a PostController file in the app/Http/Controllers directory. In this controller class, we can make the CRUD methods in laravel.
In this controller class, we will create only the get and delete records methods for this example instead of creating the whole CRUD operation. You can create other methods for inserting, updating, etc.
Let’s create the required methods for implementing the soft delete functionality in laravel application.
To display the records in laravel blade view, we will create an
index() method. You can take any name of this method but if you have created the resource controller, the methods will be already there.
index() method – to display the all records (trashed or non-trashed) based on condition.
In the above
index() method, we get the post records from the database and make a condition based on the soft delete feature.
To delete the record, we will create a
destroy() method in PostController class. This method will delete the record by ID and marked as a soft delete in the database by updating the value of the “deleted_at” column. It will not delete the record permanently.
destroy() method – to delete the record by ID but not permanently.
Force Delete Record
Now we will make another delete method which will delete the record permanently from the database. In this method, we will use the
forceDelete() method in the query to delete the record permanently.
forceDelete() method – to delete the record permanently from database.
So above we have created the methods for deleting the records, now we will make methods for restoring them. It will only work in a soft delete scenario.
It will be useful to keep the backup of records and also if anyone has deleted the records by mistake or accidentally. With the help of this method, you can restore the records.
restore() method – to restore the records that you have soft deleted.
Restore All Records
Restoring one by one is a bit itchy and time-consuming because you have to perform an action on all the records one by one. But it could be easy by making another method.
Let’s say you want to restore all the deleted records in one action then you have to make another method that will restore all the records. See the following code.
restoreAll() method – to restore all the records in one action.
Now, put all your above methods in PostController class and save it. You can change these methods as per your requirements.
Step 6: Create Resource Routes
Now it’s time to create the routes. To create routes open the routes/web.php file and add the following code to this file.
Routing in laravel allows us to link all the application actions to an appropriate controller and return the result based on it.
Step 7: Create Blade View
Now, that it’s done with the laravel backend functionality part, now we will make a frontend view part to display all the records and perform all the above actions which we have done for soft delete.
Let’s create a blade file posts.blade.php in resources/views directory. And implement the laravel soft delete feature actions at the frontend side.
Now, it’s all done, you have completed the laravel soft delete feature example tutorial. You can test it now by inserting the dummy data.
To start the laravel server, run the following command.
It will start the server on default port like http://localhost:3000. Enter this URL http://localhost:3000/posts in your browser and test with soft delete functionality.
So in this tutorial, you learned about the laravel soft delete feature and how to implement it in laravel application. In addition, you also learned how you can restore the data from database in case of soft delete.
Hope you like this tutorial, please share it with your friends or in groups to help someone else. If you have any queries please ask me in the comment section, I will respond to you as soon as possible.