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

The Strategic Leader’s Guide to Choosing Scalable Workflow Orchestration Tools

Introduction Modern data architecture is growing more decentralized and complex by the day. Organizations no longer pull data from a single transactional database into an isolated local…

Read More

Modern Data Operations: A Practical DataOps Platform Implementation Guide

Introduction Modern data ecosystems are expanding at an unprecedented rate. Centralized databases have given way to distributed cloud data warehouses, real-time data streaming architectures, and multi-cloud data…

Read More

Data Pipeline Optimization Techniques for Low-Latency Data Analytics

Introduction In a fast-paced digital economy, the shelf life of data value is shorter than ever. Businesses no longer have the luxury of waiting for overnight batch…

Read More

The Best AIOps Training Program Guide For Cloud Engineers

As modern IT environments transition from centralized datacenters to highly distributed, multi-cloud, and microservices-based setups, the sheer volume of data generated by enterprise software has exploded. Infrastructure…

Read More

Connect Directly with Trusted Local Experts Using Professnow Marketplace

The local service market is highly fragmented, making it difficult to verify a provider’s background, past work, or true capabilities before they show up at your door….

Read More

Accelerating Analytics Delivery by Automating Data Validation with DataOps Tools

Introduction In the modern digital economy, high-quality, trusted data serves as the foundation for critical enterprise decisions. Organizations rely heavily on business intelligence, machine learning models, and…

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