apply soft delete by custom laravel

Solutions on MaxInterview for apply soft delete by custom laravel by the best coders in the world

showing results for - "apply soft delete by custom laravel"
Emma
18 Jan 2018
1Inside Migrations : 
2=====================
3  
4public function up()
5{
6  Schema::create('users', function (Blueprint $table) {
7	.........
8    .........
9    $table->timestamp('deleted_at')->nullable();
10   	...........
11  });
12}
13
14Inside Models : 
15=================
16
17class User extends Authenticatable
18{  
19  ..........
20  protected static function boot()
21  {
22    parent::boot();
23    static::addGlobalScope(new CustomSoftDeleteScope);
24  }
25  .......
26}
27
28Add a global scope file :
29==========================
30  
31<?php
32
33namespace App\Scopes;
34
35use Illuminate\Database\Eloquent\Builder;
36use Illuminate\Database\Eloquent\Model;
37use Illuminate\Database\Eloquent\Scope;
38
39class CustomSoftDeleteScope implements Scope
40{
41    /**
42     * Apply the scope to a given Eloquent query builder.
43     *
44     * @param \Illuminate\Database\Eloquent\Builder $builder
45     * @param \Illuminate\Database\Eloquent\Model $model
46     * @return void
47     */
48    public function apply(Builder $builder, Model $model)
49    {
50        $builder->whereNull('deleted_at');
51    }
52}