BaseClearModel::scopeDraft()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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