Laravel 8 Soft Delete Tutorial Example

May 21, 2021 . Admin

Hi Guys,

Today, i will learn you how to use soft delete in laravel 8,we are tell simple example of laravel 8 soft delete.it will laravel 8 soft delete migration.

How To Use Laravel Soft Delete

There are only few simple steps to use soft delete in your project:

  • Add deleted_at
    column in migration using $table->softDeletes();
  • To enable soft deletes for a model, integrate the Illuminate\Database\Eloquent\SoftDeletes trait to the model and utilize it by utilizing use keyword like use SoftDeletes

Now, when you call the expunge method on the model, the deleted_at column will be set to the current date and time.

we abstract row from database when expunge record from site. But laravel 8 introduce SoftDeletes in models that way we can't abstract from database but if abstract record from front side then it doesn't show record on front. So we can retrieve record from database if we abstract erroneous row.

How work soft delete, laravel integrate deleted_at column on the table that be default will be null and when we abstract then it will place current timestamp, Laravel Model always fetch that record have only deleted_at = null.

In this tutorial i am utilizing Utilizer model to our laravel soft expunge example. To enable soft effaces for a model, utilize the Illuminate\Database\Eloquent\SoftDeletes trait on the model:

So, how to utilize in our project, so first when you engender table migration then you have to integrate softDeletes(). you can visually perceive like bellow example of migration.

Here i bellow example you can learn soft delete in laravel 8 follow follow my steps.

Let's see full example bellow:

Create products Migration Soft Delete

In this post migration open product migration file and put the below same code.

Running the command in step 2 will engender a migration file in database/migrations/, edit the file and integrate $table->softDeletes(); to the up() function and withal $table->dropSoftDeletes(); to the down() function, our migration file looks akin to this

Path : database/migrations/2021_05_21_095534_create_products_table.php
<?php

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

class CreateProductTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */

    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('price');
            $table->text('details');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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

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

Create Product Model Soft Delete

Next, go to app/Product.php and open Product model file and put the below same code. now you can find deleted_at column in your products table and you have also model should look like as bellow

here following path of model fille

Path: app/Product.php
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use HasFactory;
    use SoftDeletes;

    /**
    * fillable your column
    *
    * @var array
    */

    protected $fillable = ['name','price','details'];

    /**
    * The attributes that should be mutated to dates.
    *
    * @var array
    */
    protected $dates = ['deleted_at'];
}
Get All Records

Now, you can use Product model as normally like you use before, you get all record like this way.

$data = Product::get();
Delete one Record

It will return all record that have deleted_at = null only and you can also remove record like this way:

Now time to check soft delete in laravel 8, so retrieve this following data and efface it like below image utilizing following command

$data = Product::find(1)->delete();
Get All Records After Delete one Record

Now, you can utilize Product model as mundanely like you utilize before, you get all record After Expunge one Record like this way.

Boom, our expunged data is here. So we can get back our effaced data utilizing withTrashed(). but do you, we can aslo instaurate it and can back it with mundane eloquent query. Just we have to renovate it for doing it.

$data = Product::withTrashed()->get();

You can check the table records.

It will help you..

#Laravel 8 #Laravel