How to use soft delete in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now here in this post i will show you How to use soft delete in Laravel? Normally, when we are delete any data from our front end then the row from our database also delete from our Databse. But we are know that laravel has introduce with us SoftDeletes in models on that way we can delete our data from front end but row are not from database . we can remove record from our front side then it doesn’t show the record on front. So on that way we can easily retrieve the record from database if we are remove any wrong row.

Here we are know that How to work soft delete. For using softdelete in laravel we need to add deleted_at column on the table on that will be default null value and when we are delete any data from front end then it will be place by current timestamp. When Laravel Model fetch data then it always fetch that record which have only deleted_at  is null.

So, now we will know here how to use in our project, so at first we need to create a table by using migration then we need to add the softDeletes(). Now we have to see like as like as in the bellow example of migration.

Migration Example:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function(Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->softDeletes();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop("items");
    }
}

Okay, now we can find a deleted_at column in our items table and we need to also use model should look like as bellow:

Items Model

namespace App\Models;


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


class Item extends Model
{
    use SoftDeletes;

    
    public $fillable = ['title','description'];


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

Now, we can easily use the Item model as normally like we are use before, we get all record as like as this way.

$data = Item::get();

Now, we can also use Item model as normally as like we use before, we get all the record like this way.

$data = Item::get();

Read Also : How to Use Group by year month using Laravel Query Builder ?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →