Completed
Push — widgets-pivot ( dc1e33 )
by Tony
02:59
created

Dashboard::scopeWithGlobal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * App\Models\Dashboard
10
 *
11
 * @property integer $dashboard_id
12
 * @property integer $user_id
13
 * @property string $dashboard_name
14
 * @property integer $access
15
 * @property-read \App\Models\User $user
16
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UsersWidgets[] $widgets
17
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Dashboard whereDashboardId($value)
18
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Dashboard whereUserId($value)
19
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Dashboard whereDashboardName($value)
20
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Dashboard whereAccess($value)
21
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Dashboard allAvailable($user)
22
 * @mixin \Eloquent
23
 */
24
class Dashboard extends Model
25
{
26
    /**
27
     * Indicates if the model should be timestamped.
28
     *
29
     * @var bool
30
     */
31
    public $timestamps = false;
32
    /**
33
     * The table associated with the model.
34
     *
35
     * @var string
36
     */
37
    protected $table = 'dashboards';
38
    /**
39
     * The primary key column name.
40
     *
41
     * @var string
42
     */
43
    protected $primaryKey = 'dashboard_id';
44
    /**
45
     * The attributes that are mass assignable.
46
     *
47
     * @var array
48
     */
49
    protected $fillable = ['user_id', 'dashboard_name', 'access'];
50
51
    /**
52
     * Initialize this class
53
     */
54
    public static function boot()
55
    {
56
        parent::boot();
57
58
        static::deleting(function (Dashboard $dashboard) {
59
            // delete related data
60
            $dashboard->widgets()->delete();
61
        });
62
    }
63
64
    // ---- Query scopes ----
65
66
    /**
67
     * @param Builder $query
68
     * @return Builder
69
     */
70
    public function scopeWithGlobal(Builder $query)
71
    {
72
        return $query->orWhere('access', '>', 0);
73
    }
74
75
    // ---- Define Reletionships ----
76
77
    /**
78
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
79
     */
80
    public function user()
81
    {
82
        return $this->belongsTo('App\Models\User', 'user_id');
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
87
     */
88
    public function widgets()
89
    {
90
        return $this->hasMany('App\Models\UsersWidgets', 'dashboard_id');
91
    }
92
}
93