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

Strategic DevOps Career Growth and High Salary Skills

Introduction The digital landscape is shifting rapidly. As companies across the globe transition to cloud-native infrastructures, the demand for professionals who can bridge the gap between development…

Read More

Top DevOps Certifications: Dominate Kubernetes, Cloud, And Automation

Introduction The cloud infrastructure world is moving faster than ever, and the demand for production-ready engineering talent is breaking records. Teams everywhere are desperately trying to bridge…

Read More

Streamlining Distributed Pipelines with DataOps Multi-Cloud Data Management

Introduction Modern business operations generate massive amounts of information every single second. To store, process, and analyze this information, organizations no longer rely on a single data…

Read More

Ultimate DataOps Automation Tools Guide: Build and Orchestrate Scalable Pipelines

Introduction Modern enterprises run on data, yet managing the underlying infrastructure remains a massive operational challenge. Historically, data workflows were handled manually. Data engineers wrote custom scripts,…

Read More

Accelerate Your Pipeline: Implementing Real-Time DataOps

Introduction Real-time DataOps is a critical evolution in how modern organizations manage the constant flow of information. By integrating automation, continuous testing, and real-time processing, businesses can…

Read More

Calculate Your Canada PR Points: The Complete Guide to Boosting Your CRS Score

Introduction Canada uses an objective, merit-based points system to select the most qualified candidates from around the world. To assess your chances, you need to use a…

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