TL;DR Laravel's SoftDeletes trait enables "deleting" records without removing them from the database by adding a flag/column to indicate deletion status. The trait can be added to models, requiring a 'deleted_at' timestamp column in the database table.
Eloquent Soft Deletes with Use SoftDeletes Trait
As a Laravel developer, you're likely familiar with Eloquent's powerful ORM capabilities. But have you ever encountered situations where you need to delete data without actually deleting it? Perhaps you want to maintain a record of deleted items or allow users to recover accidentally deleted content.
This is where soft deletes come in – a feature that enables you to "delete" records while still keeping them in the database. In this article, we'll explore how to use Laravel's SoftDeletes trait to implement soft deleting in your applications.
What are Soft Deletes?
Soft deletes allow you to mark a record as deleted without actually removing it from the database. This is achieved by adding a flag or column to indicate whether an item has been "deleted". When a record is marked for deletion, its corresponding value in this flag/column is set to 1 (or some other indicator), effectively hiding the record from views and queries.
The SoftDeletes Trait
Laravel provides a trait called SoftDeletes that simplifies the implementation of soft deleting. To use it, you need to add it to your model file by adding the following line at the top:
use Illuminate\Database\Eloquent\SoftDeletes;
Next, include the trait in your model's class definition:
class YourModel extends Model {
use SoftDeletes;
}
Configuring Soft Deletes
Once you've added the SoftDeletes trait to your model, you need to configure it. Laravel requires a date column named deleted_at to store the timestamp when an item was deleted.
To add this column, run the following migration:
Schema::table('your_table_name', function (Blueprint $table) {
$table->softDeletes();
});
This will create a deleted_at timestamp in your database table. You can customize the date format using the $dates property on your model.
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
Using Soft Deletes
Now that you've set up soft deleting, you can use it in your controllers and queries. When an item is deleted using the delete method, its corresponding value in the deleted_at column will be updated.
YourModel::find(1)->delete();
To retrieve all items, including those marked for deletion:
YourModel::withTrashed()->get();
Conversely, to retrieve only active (i.e., not deleted) items:
YourModel::withoutTrashed()->get();
Conclusion
Soft deletes are a powerful feature in Laravel that allows you to maintain records of deleted items while keeping your database clean. With the SoftDeletes trait and a few configuration tweaks, you can easily implement soft deleting in your applications.
In this article, we've covered the basics of soft deletes, including how to add the SoftDeletes trait to your model and configure it to work with your database table. Whether you're working on a large-scale application or a small project, understanding soft deletes will help you build more robust and scalable systems.
