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 …!!!!

Related Posts

Start a Blog for Free: Step-by-Step Beginner Guide

Blogging remains one of the most powerful mediums for sharing ideas, building an online presence, and establishing authority in any field. Whether you want to document your…

Read More

The Complete Guide to Free Blog Hosting for Creative Writers

INTRODUCTION In an era dominated by fast-paced social media feeds and endless algorithm updates, long-form content creation continues to stand as the cornerstone of thoughtful communication, personal…

Read More

Top Pediatric Cardiac Hospitals and Expert Heart Specialists

INTRODUCTION Choosing where to undergo cardiac treatment is one of the most significant healthcare decisions a patient or family will ever make. Heart conditions demand swift intervention,…

Read More

Discover How Tool Selection Impacts DataOps Success and Team Productivity

Modern businesses run on data. Every click, sale, and customer interaction generates information that companies use to make decisions. However, raw data is like crude oil; it…

Read More

Strategies to Monitor Multi-Cloud Data Pipelines and Prevent Failures

Introduction Today, data drives almost every major business decision. To keep up with massive amounts of information, organizations no longer rely on just one cloud provider. Instead,…

Read More

Improving Enterprise Data Governance Strategy Through Modern DataOps Platform Tools

Introduction Modern organizations run on data. Every transaction, customer click, and supply chain adjustment generates valuable information. However, managing this information at scale presents significant challenges. If…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x