BaseClearModel   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 40
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 14 3
A scopeDraft() 0 3 1
A creators() 0 4 1
A scopeActive() 0 3 1
A editors() 0 4 1
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
// ======= Base model without SoftDeletes
8
9
class BaseClearModel extends Model
10
{
11
    //scopes
12
    public function scopeActive($query)
13
    {
14
        return $query->where('active', true);
15
    }
16
17
    public function scopeDraft($query)
18
    {
19
        return $query->where('active', false);
20
    }
21
22
    public function creators()
23
    {
24
        return $this->belongsTo(User::class, 'created_by', 'id')
25
      ->withDefault(['name' => '-']);
26
    }
27
28
    public function editors()
29
    {
30
        return $this->belongsTo(User::class, 'modifed_by', 'id')
31
      ->withDefault(['name' => '-']);
32
    }
33
34
    //observers
35
    public static function boot()
36
    {
37
        parent::boot();
38
39
        self::creating(function ($model) {
40
            if (Auth()->check()) {
41
                $model->created_by = Auth()->user()->id;
42
                $model->modifed_by = Auth()->user()->id;
43
            }
44
        });
45
46
        self::updating(function ($model) {
47
            if (Auth()->check()) {
48
                $model->modifed_by = Auth()->user()->id;
49
            }
50
        });
51
    }
52
}
53