custom constant soft delete laravel code example

Example: apply soft delete by custom laravel

Inside Migrations : 
=====================
  
public function up()
{
  Schema::create('users', function (Blueprint $table) {
	.........
    .........
    $table->timestamp('deleted_at')->nullable();
   	...........
  });
}

Inside Models : 
=================

class User extends Authenticatable
{  
  ..........
  protected static function boot()
  {
    parent::boot();
    static::addGlobalScope(new CustomSoftDeleteScope);
  }
  .......
}

Add a global scope file :
==========================
  
<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class CustomSoftDeleteScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->whereNull('deleted_at');
    }
}

Tags:

Php Example