Sling Academy
Home/PHP/Explore soft deletes in Eloquent: A developer’s guide

Explore soft deletes in Eloquent: A developer’s guide

Last updated: January 16, 2024

Introduction

Soft deletes are a critical feature when it comes to managing deletion of records in web applications. Instead of permanently removing an entry from the database, a soft delete marks a record as ‘deleted’ without actually removing it. This allows for recovery of records at a later point if necessary. Laravel’s Eloquent ORM supports soft deletes out of the box, providing developers a convenient and efficient way to handle such scenarios. This tutorial explores the implementation and use of soft deletes in Eloquent.

Enabling Soft Deletes

Soft deleting functionality is enabled by including the SoftDeletes trait in your Eloquent model. Doing so adds a deleted_at column to your table schema which indicates if a row is soft deleted.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
}

You’ll also need to add the deleted_at column to your database table, which can be done via a migration:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSoftDeletesToPosts extends Migration
{
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropSoftDeletes();
        });
    }
}

Using Soft Deletes

Once soft deletes have been set up, deleting a model is done using the same delete() method as for a hard delete:

$post = Post::find(1);
$post->delete();

This won’t remove the record from the database but will fill the deleted_at column with the current timestamp. The Eloquent query builder will automatically exclude soft deleted models from the query results:

$posts = Post::all();

The above query will only return posts that are not soft deleted.

Retrieving Soft Deleted Models

To include soft deleted models in a result set, use the withTrashed() method:

$posts = Post::withTrashed()->get();

If you need to retrieve only the soft deleted models, you can use the onlyTrashed() method:

$deletedPosts = Post::onlyTrashed()->get();

Restoring Soft Deleted Models

Soft deleted models can be restored using the restore() method:

$post->restore();

The restore() method clears the deleted_at column, bringing the soft deleted model back into the normal dataset.

Permanently Deleting Models

To permanently delete a soft deleted model, use the forceDelete() method:

$post->forceDelete();

Querying Relations with Soft Deletes

Soft delete functionality is also available when querying relations. To include related models with soft deletes, use the withTrashed():

$comments = $post->comments()->withTrashed()->get();

To get only the deleted relations, append the onlyTrashed() method:

$deletedComments = $post->comments()->onlyTrashed()->get();

Advanced Usage: Customizing The Deleted At Column

Eloquent allows you to customize the name of the deleted_at column used by the SoftDeletes trait. You can achieve this by overriding the DELETED_AT constant in your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;

    const DELETED_AT = 'archive_at';
}

Conclusion

Soft deletes in Eloquent provide a robust solution for managing record deletion while maintaining the integrity of data. They offer an essential set of functionalities for building resilient web applications that handle data revisions and recoveries gracefully.

Next Article: How to Restore Soft Deleted Models in Eloquent

Previous Article: Weighted random selection in Eloquent: A Practical Guide

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array