1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Users\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
8
|
|
|
use Sfneal\Models\Model; |
9
|
|
|
use Sfneal\Scopes\OrderScope; |
10
|
|
|
use Sfneal\Users\Builders\RoleBuilder; |
11
|
|
|
use Sfneal\Users\Factories\RoleFactory; |
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
|
|
|
protected 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
|
|
|
* @return RoleBuilder |
71
|
|
|
*/ |
72
|
|
|
public function newEloquentBuilder($query): RoleBuilder |
73
|
|
|
{ |
74
|
|
|
return new RoleBuilder($query); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Custom Role query Builder. |
79
|
|
|
* |
80
|
|
|
* @return RoleBuilder|Builder |
81
|
|
|
*/ |
82
|
|
|
public static function query(): RoleBuilder |
83
|
|
|
{ |
84
|
|
|
return parent::query(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* 'users' with roles. |
89
|
|
|
* |
90
|
|
|
* @return HasMany |
91
|
|
|
*/ |
92
|
|
|
public function users() |
93
|
|
|
{ |
94
|
|
|
return $this->hasMany(User::class, 'role_id', 'role_id') |
95
|
|
|
->where('type', '=', 'user'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve the 'class' attribute with a default value. |
100
|
|
|
* |
101
|
|
|
* @param null $value |
|
|
|
|
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getClassAttribute($value = null): string |
105
|
|
|
{ |
106
|
|
|
return 'label label-'.($value ?? 'default'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|