Laravel provides a built-in feature that allows us to flag database rows as deleted without actually deleting them from the database. This article discusses how we can get started using them.
Why Should I Use Soft Deletes?
As we’ve discussed in “Stop Deleting Data”, when we
DELETE a row from the database it’s gone forever without going through a potentially painful process of doing a full database restore. Soft deleting the data allows you to easily restore the data with minimal work and can be a huge time saver when a user accidentally deletes some data.
Laravel provides support for soft deleting using the
The first part of the process we need to tackle is setting up our database tables to have the
Adding the Soft Delete Columns to a New Table
Let’s start by creating a new model to track a
Project in our project management application. We’ll create the model and migration in one step so we can be as lazy as possible.
Now when we open the newly created migration we’ll add the following to lines to our
It looks like this.
$table->softDeletes(); function call is what sets up the table to allow for the
SoftDeletes trait to work. Without it, we’ll get query errors.
Now we’ll run the migration.
Let’s look at what the table looks like inside MySQL.
As we can see there’s a
deleted_at column in the table definition. This is what the
$table->softDeletes(); call added to the table and what the
SoftDeletes trait will use to track if the row has been deleted.
Adding the Soft Delete Columns to an Existing Table
Let’s look at how we can add the soft delete columns to an existing table. We’re going to create a new migration using the
make:migration command to add the columns to the
users table because out of the box Laravel doesn’t have this column on the
Next we’re going to alter the migration so it both adds the columns in the
up() function (
$table->softDeletes();) and removes them in the
down() function (
Again, we’ll run the migration.
Setting Up the Model to Use Soft Deletes
Now that we have our database tables set up we can start working with soft deleted models in our code. The first step is adding the
Illuminate\Database\Eloquent\SoftDeletes trait to the models. Below is an example model where we have set it up to use the
It’s important to note that even though we added the
SoftDeletes column to our model Laravel doesn’t automatically use it until we add the trait so we will still irreparably delete data without it.
Deleting a Model
Now that everything is setup let’s test it to see what happens.
First we’ll use tinker to create a new
When we check the database we can see that it’s been persisted to the database and the
deleted_at column is set to
null indicating that it hasn’t been deleted.
Now we’ll delete the model.
And back in MySQL, we can see
deleted_at is no longer
null which indicates it has been deleted.
Restoring a Model
If you accidentally delete the model Laravel makes it easy to restore the record using the
Deleting a Model
Let’s say you have a case where someone accidentally entered information and we need to delete the record from the database.
SoftDeletes provides the
forceDelete() function that will do just that.
Finding a Deleted Model
What happens when we delete a model and need to find it later?
First, let’s set up a new
Project and soft delete it.
When we attempt to use
findOrFail() we’ll receive a
ModelNotFoundException because the
SoftDeletes trait is filtering them out.
To get around this we need to call the
withTrashed() function before we call
Finding a Deleted Model In a Relationship
The other part of our code we need to be on the lookout for is when we have a model that defines an Eloquent relationship with the
Normally, we would define our relationship like so.
User associated with this
Project is soft-deleted and we attempt to access it through this relationship the function will return
The solution to this again is to use the
withTrashed() function to have it return a result.
Let us know in the comments if this has been helpful and if you’re finding soft deletes useful.
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.