Home » How To Use The Laravel Soft Delete

How To Use The Laravel Soft Delete

Last updated on January 4, 2021 by

In this tutorial, we will learn how to use the Laravel soft delete concept using examples. As the name suggests soft delete is used for deleting record visually that means you can’t see the record on your front-end but those records not actually removed from the database. Let’s understand the soft delete.

Table of Contents
1. How Laravel Soft Delete Works
2. How To Use Laravel Soft Delete
3. Example Of Soft Delete

How Laravel Soft Delete Works

When we use soft delete in the project then records are not actually removed from the database. Instead of that timestamp has been assigned to the deleted_at column. Also, when you querying the model that uses soft deletes then Laravel retrieves those records which timestamps are nulled in deleted_at column.

How To Use Laravel Soft Delete

There are only two simple steps to enable soft delete. They are:

  1. Add deleted_at column in migration using $table->softDeletes();
  2. To enable soft deletes for a model, add the Illuminate\Database\Eloquent\SoftDeletes trait to the model and use it by using use keyword like use SoftDeletes;

Now, when you call the delete method on the model, the deleted_at column will be set to the current date and time.

That’s it. Let’s see the example.

Example Of Soft Delete

1. Set up your new migration table to like this

<?php

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

class CreateProductTable extends Migration
{
    /**
     * Run the migrations.
     * @scratchcode.io
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->text('description');
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @scratchcode.io
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

Next, you should set up a new migration table. Migrate the table by running.

php artisan migrate

Then you can go to the database and you will notice the deleted_at column

2. Now, set up your Model to like this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //add this line

class Product extends Model
{
    use SoftDeletes; //add this line

    public $fillable = [ 'name', 'description' ];

    /**
     * The attributes that should be mutated to dates.
     * scratchcode.io
     * @var array
     */

    protected $dates = [ 'deleted_at' ];

}

3. Now, run the delete method

Now, when you call the delete method on the model, the deleted_at column will be set to the current timestamp. When querying a model that uses soft deletes, the “deleted” records will not be included in query results. To soft delete a model you may use:

$product = Product::find( $id );
$product->delete();

Additionally, read our guide:

  1. Best Way to Remove Public from URL in Laravel
  2. Run PHP Artisan Commands On Shared Hosting Servers
  3. How To Calculate Age From Birthdate
  4. Active Directory Using LDAP in PHP or Laravel
  5. Laravel: Increase Quantity If Product Already Exists In Cart
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Between Events and Observers In Laravel
  10. Session Not Working In Laravel
  11. Laravel 8: Target Class Controller Does Not Exist

That’s it for now. We hope this article helped you to learn soft deleting in Laravel.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment