Answers for "apply soft delete by custom laravel"

PHP
0

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');
    }
}
Posted by: Guest on November-28-2020

Browse Popular Code Answers by Language