|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Users\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Database\Factories\RoleFactory; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
9
|
|
|
use Sfneal\Models\Model; |
|
10
|
|
|
use Sfneal\Scopes\OrderScope; |
|
11
|
|
|
use Sfneal\Users\Builders\RoleBuilder; |
|
12
|
|
|
|
|
13
|
|
|
class Role extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
use HasFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The "booting" method of the model. |
|
19
|
|
|
* |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function boot() |
|
23
|
|
|
{ |
|
24
|
|
|
parent::boot(); |
|
25
|
|
|
|
|
26
|
|
|
// Global scopes |
|
27
|
|
|
static::addGlobalScope(new OrderScope('order', 'asc')); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected $table = 'role'; |
|
31
|
|
|
protected $primaryKey = 'role_id'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The attributes that are mass assignable. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $fillable = [ |
|
39
|
|
|
'role_id', |
|
40
|
|
|
'type', |
|
41
|
|
|
'name', |
|
42
|
|
|
'description', |
|
43
|
|
|
'class', |
|
44
|
|
|
'order', |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The attributes that should type cast. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $casts = [ |
|
53
|
|
|
'order' => 'int', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create a new factory instance for the model. |
|
58
|
|
|
* |
|
59
|
|
|
* @return RoleFactory |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function newFactory(): RoleFactory |
|
62
|
|
|
{ |
|
63
|
|
|
return new RoleFactory(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Query Builder. |
|
68
|
|
|
* |
|
69
|
|
|
* @param $query |
|
70
|
|
|
* |
|
71
|
|
|
* @return RoleBuilder |
|
72
|
|
|
*/ |
|
73
|
|
|
public function newEloquentBuilder($query): RoleBuilder |
|
74
|
|
|
{ |
|
75
|
|
|
return new RoleBuilder($query); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Custom Role query Builder. |
|
80
|
|
|
* |
|
81
|
|
|
* @return RoleBuilder|Builder |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function query(): RoleBuilder |
|
84
|
|
|
{ |
|
85
|
|
|
return parent::query(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* 'users' with roles. |
|
90
|
|
|
* |
|
91
|
|
|
* @return HasMany |
|
92
|
|
|
*/ |
|
93
|
|
|
public function users() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->hasMany(User::class, 'role_id', 'role_id') |
|
96
|
|
|
->where('type', '=', 'user'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve the 'class' attribute with a default value. |
|
101
|
|
|
* |
|
102
|
|
|
* @param null $value |
|
|
|
|
|
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getClassAttribute($value = null): string |
|
107
|
|
|
{ |
|
108
|
|
|
return 'label label-'.($value ?? 'default'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|