How To

How to Use Soft Delete in Laravel 8?

Hello Laravel Developers,

Today we will learn about Soft Delete in Laravel 8.

Data is crucial for any Laravel project. Sometimes we accidentally delete some data, which means it is gone permanently and we cannot restore it. This will create issues if the data is used in other project parts. To avoid issues, there comes the need for Soft Delete.

Let’s understand what soft delete is and why it is used

What is Soft Delete in Laravel?

The soft delete feature in Laravel never deletes data from the database. It just prevents the data from showing. The soft delete data can be restored back or permanently deleted from the database.  

Why do we need to Use Soft Delete in Laravel?

As mentioned, soft delete does not actually remove data from the database. A deleted attribute is set on the model that indicates the date and time when it was deleted. For the purpose of data safety and backup, we use the soft delete in Laravel.

Let’s see how to use soft delete in Laravel.

Steps to Implement Soft Delete in Laravel 8:

Step 1: Create Migration 

<?php

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

class CreateCompaniesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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

Now you can find the deleted_at column in your companies table.

Step 2: Create a Model

To enable soft deletes for a model, add the 

Illuminate\Database\Eloquent\SoftDeletes trait to the model:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
 
class Company extends Model
{
    use SoftDeletes;
    
    public $fillable = ['title'];
}

The SoftDeletes trait will automatically cast the deleted_at attribute to a DateTime / Carbon instance for you.

Step 3: Get Records

You can get all records like as below, 

$companies = Company::get();

It will return all records that have deleted_at = null only

You can also get deleted records with soft delete.

$company = Company::withTrashed()->get();

Conclusion:

Hence, it was all about using Soft Delete in Laravel 8. Avoid permanent loss of your critical data by implementing the Laravel soft delete feature. Share the article with your friends and stay in touch with us. 

Happy Coding!

Click to rate this post!
[Total: 19 Average: 4.3]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

2 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago