When developing applications, deleting records permanently isn’t always the best choice. You might want to give users the option to restore data that was accidentally deleted or keep track of deleted records for future auditing. This is where soft deletes in Laravel come into play. Instead of permanently removing a record from the databas, Laravel allows you to “soft delete” it which simply means marking it as deleted while retaining the data in the database.
In this article, we’ll walk you through how to implement soft deletes in Laravel, offering a safer way to handle record deletions.
What is Soft Delete in Laravel?
Soft deletes allow you to keep deleted records in the database by marking them as deleted, rather than actually removing them. Laravel achieves this by adding a special deleted_at
column to your database table. When a record is soft deleted, Laravel sets this column with a timestamp, indicating the record’s deletion time. The record stays in the database but will be excluded from normal queries unless explicitly included.
Step 1: Add Soft Deletes to Your Model
To implement soft deletes, start by adding the SoftDeletes trait to your Eloquent model. This trait handles the logic of managing the deleted_at
field.
Here’s an example of how to add soft deletes to your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
}
The use SoftDeletes;
statement tells Laravel to manage soft deletes for this model.
Step 2: Update Your Database Schema
To track soft deleted records, you need to add a deleted_at
column to your database table. You can do this by updating your migration file.
Here’s how to modify an existing table to include the deleted_at
column:
Schema::table('posts', function (Blueprint $table) {
$table->softDeletes(); // Adds the `deleted_at` column
});
If you’re creating a new table, include the softDeletes()
method in the migration like this:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
$table->softDeletes(); // Adds the `deleted_at` column
});
After updating the migration file, run the migration using:
php artisan migrate
This will add the deleted_at
column to your table.
Step 3: Soft Deleting a Record
Now that your model and database are set up, you can soft delete records by calling the delete()
method on an Eloquent model. This method doesn’t actually remove the record from the database; it simply sets the deleted_at
column.
Here’s an example of soft deleting a record:
$post = Post::find(1);
$post->delete();
After calling the delete()
method, the deleted_at
column will be filled with the current timestamp.
Step 4: Querying Soft Deleted Records
By default, Laravel will exclude soft deleted records from query results. For example:
$posts = Post::all(); // Returns only records where `deleted_at` is NULL
If you want to include soft deleted records in your queries, you can use the withTrashed()
method:
$posts = Post::withTrashed()->get(); // Includes soft deleted records
To only retrieve soft deleted records, use the onlyTrashed()
method:
$deletedPosts = Post::onlyTrashed()->get(); // Returns only soft deleted records
Step 5: Restoring Soft Deleted Records
If you want to restore a soft deleted record, Laravel provides the restore()
method. This will remove the timestamp from the deleted_at
column and make the record visible again.
Here’s how to restore a soft deleted record:
$post = Post::withTrashed()->find(1);
$post->restore();
The restore()
method works similarly to delete()
, but instead of adding a timestamp, it sets the deleted_at
column to NULL
, bringing the record back to life.
Step 6: Permanently Deleting a Record
Sometimes, you may need to permanently delete a record from the database, bypassing the soft delete functionality. Laravel provides the forceDelete()
method for this:
$post = Post::withTrashed()->find(1);
$post->forceDelete();
This will permanently remove the record from the database and it cannot be restored.
Implementing soft deletes in Laravel is a simple yet powerful way to manage record deletions safely. It allows you to retain deleted data for future recovery or auditing, providing greater flexibility when handling data in your application.
See Also: