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

Exploring Financial Operations Workflows in Modern Cloud Environments

Introduction The Certified FinOps Professional is the definitive benchmark for experts looking to master the intersection of finance, engineering, and business. As organizations transition from traditional data…

Read More

Strategic Certified FinOps Engineer integrates governance with cloud operations

Introduction The shift to cloud computing has fundamentally altered how businesses manage infrastructure, but it has also introduced significant financial complexities that many engineering teams struggle to…

Read More

Certified FinOps Manager Knowledge for Cloud Financial Governance

Introduction The shift toward cloud-native infrastructure has brought undeniable speed, but it has also introduced significant financial complexity. The Certified FinOps Manager is a professional designation designed…

Read More

Smart Career Growth Through Certified FinOps Architect Learning Journey

Introduction The Certified FinOps Architect is a professional certification designed to help engineers, cloud professionals, and managers optimize cloud financial operations and cost efficiency. This guide is…

Read More

CDOM – Certified DataOps Manager Learning Path for Modern Data Professionals

Introduction The CDOM – Certified DataOps Manager is a professional designation designed to bridge the gap between data engineering and operational excellence. This guide is written for…

Read More

Professional development journey using CDOA – Certified DataOps Architect

Introduction The CDOA – Certified DataOps Architect is a professional designation designed to address the unique challenges of managing and scaling data delivery in cloud-native environments. This…

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