How to use Soft Delete in Laravel?

Introduction

Hey there! Are you interested in learning how to use soft delete in Laravel? Well, you’ve come to the right place. In this blog post, we’ll dive into the world of soft delete and explore its functionality in Laravel. So, let’s get started!

What is Soft Delete?

Soft delete is a feature in Laravel that allows you to delete records from a database table while still keeping them in the database. Instead of permanently deleting the record, Laravel adds a “deleted_at” column to the table and sets its value to the current timestamp. This way, you can easily restore or permanently delete the record later on.

How to Enable Soft Delete in Laravel?

To enable soft delete in your Laravel project, you need to perform the following steps:

How does soft delete, laravel adds deleted_at column on the table that be default will be null and when we remove then it will place the current timestamp, The Laravel Model always fetches that the record has only deleted_at = null.

So, how to use it in our project, so first when you create table migration then you have to add softDeletes(). you can see like below example of migration.

Migration Example:

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->enum('status', array('1','0','-1'))->default('0');
            $table->string('file_pic')->nullable();
            $table->string('password');
            $table->tinyInteger('type')->default(0);
            $table->rememberToken();
            $table->timestamps();


          
        });
    }

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

Okay, now you can find deleted_at column in your items table and you have also model should look like as below:

Users model:-

namespace App;



use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\SoftDeletes;



class User extends Model

{

    use SoftDeletes;


    

    public $fillable = ['id','name','email','etc'];



    /**

     * The attributes that should be mutated to dates.

     *

     * @var array

     */

    protected $dates = ['deleted_at'];



}

Now, you can use User model as normally as you used before, you get all records in this way.

$data = User::get();

It will return all record that have deleted_at = null only and you can also remove record like this way:

$data = User::find(1)->delete();

You can also get deleted records with soft delete.

$data = User::withTrashed()->get();

I hope this will help you …!!!!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x