I'm always excited to take on new projects and collaborate with innovative minds.
Data is one of the most valuable assets in any application, and deleting it permanently can sometimes lead to regretāor worse, disasters. Thatās why Laravelās Soft Deletes feature is a lifesaver! Soft deletes allow you to "delete" records without actually removing them from the database. This ensures data integrity and provides a way to recover accidentally deleted records. š”ļø
In this post, weāll dive into how soft deletes work in Laravel and how you can use them to safeguard your applicationās data. By the end, youāll have a solid understanding of soft deletes and their practical applications. Letās get started! šŖ
Soft deletes are a way to mark a record as deleted without actually removing it from the database. Instead of deleting the record outright, Laravel sets a deleted_at
timestamp on the record. This makes it easy to filter "deleted" records out of queries, while still retaining them for recovery or auditing purposes.
Letās walk through how to set up and use soft deletes in your Laravel application.
Ā
To enable soft deletes, add the SoftDeletes
trait to your Eloquent model. This trait provides all the necessary functionality to handle soft-deleted records.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}
Note: The SoftDeletes
trait automatically adds methods for managing soft-deleted records.
deleted_at
Column to Your Database TableSoft deletes rely on a deleted_at
column to track when a record is "deleted." Update your database migration to include this column:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->softDeletes(); // Adds 'deleted_at' column
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
php artisan migrate
Soft deleting a record sets the deleted_at
column to the current timestamp.
$post = Post::find(1);
$post->delete();
The record is now "deleted" but remains in the database.
By default, Eloquent excludes soft-deleted records from queries:
$posts = Post::all(); // Excludes soft-deleted records
To include soft-deleted records, use the withTrashed
method:
$allPosts = Post::withTrashed()->get();
To fetch only soft-deleted records, use the onlyTrashed
method:
$post = Post::onlyTrashed()->find(1);
$post->restore();
Soft-deleted records can be restored with the restore
method:
$post = Post::onlyTrashed()->find(1);
$post->restore();
This sets the deleted_at
column back to null
, effectively "undeleting" the record.
To remove a record permanently, use the forceDelete
method:
$post = Post::onlyTrashed()->find(1);
$post->forceDelete();
This completely removes the record from the database.
Soft deletes also work seamlessly with Eloquent relationships. For example, if you have a Comment
model associated with a Post
, you can configure cascading soft deletes by adding the SoftDeletes
trait to both models.
In the Post
model:
public function comments()
{
return $this->hasMany(Comment::class);
}
In the Comment
model:
use Illuminate\Database\Eloquent\SoftDeletes;
class Comment extends Model
{
use SoftDeletes;
}
When you delete a Post
, all related Comment
records can also be soft-deleted.
Soft deletes are ideal when:
However, if storage space is a concern or you donāt need to keep deleted records, you may prefer hard deletes.
Soft deletes are a powerful feature of Laravelās Eloquent ORM that helps protect your data from accidental deletion while maintaining flexibility for recovery. By implementing soft deletes in your application, you can offer a safer and more user-friendly experience.
Start using soft deletes today to safeguard your data and simplify your applicationās data management. Your future self (and your users) will thank you! š
Happy coding! š
Your email address will not be published. Required fields are marked *