|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Models\User; |
|
6
|
|
|
use A17\Twill\Models\Behaviors\HasMedias; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
9
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
10
|
|
|
use A17\Twill\Models\Behaviors\HasPermissions; |
|
11
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Role model |
|
17
|
|
|
* |
|
18
|
|
|
* @property-read string $createdAt Date of creation |
|
19
|
|
|
* @property-read string $usersCount Formatted number of users in this role (ie. '123 users') |
|
20
|
|
|
* @method static Builder published() Get published roles |
|
21
|
|
|
* @method static Builder draft() Get draft roles |
|
22
|
|
|
* @method static Builder onlyTrashed() Get trashed roles |
|
23
|
|
|
*/ |
|
24
|
|
|
class Role extends BaseModel |
|
25
|
|
|
{ |
|
26
|
|
|
use HasMedias, SoftDeletes, HasPermissions, IsTranslatable; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public $timestamps = true; |
|
29
|
|
|
|
|
30
|
|
|
protected $fillable = [ |
|
31
|
|
|
'name', |
|
32
|
|
|
'published', |
|
33
|
|
|
'in_everyone_group', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
protected $dates = [ |
|
37
|
|
|
'deleted_at', |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
public $checkboxes = ['published']; |
|
41
|
|
|
|
|
42
|
|
|
protected $casts = [ |
|
43
|
|
|
'in_everyone_group' => 'boolean', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Scope published roles. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Builder $query |
|
50
|
|
|
* @return Builder |
|
51
|
|
|
*/ |
|
52
|
|
|
public function scopePublished($query) |
|
53
|
|
|
{ |
|
54
|
|
|
return $query->wherePublished(true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Scope unpublished (draft) roles. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Builder $query |
|
61
|
|
|
* @return Builder |
|
62
|
|
|
*/ |
|
63
|
|
|
public function scopeDraft($query) |
|
64
|
|
|
{ |
|
65
|
|
|
return $query->wherePublished(false); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Scope trashed roles. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Builder $query |
|
72
|
|
|
* @return Builder |
|
73
|
|
|
*/ |
|
74
|
|
|
public function scopeOnlyTrashed($query) |
|
75
|
|
|
{ |
|
76
|
|
|
return $query->whereNotNull('deleted_at'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* User model relationship |
|
81
|
|
|
* |
|
82
|
|
|
* @return BelongsToMany|Collection|User[] |
|
83
|
|
|
*/ |
|
84
|
|
|
public function users() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->hasMany(User::class); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return the formatted created date |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getCreatedAtAttribute($value) |
|
95
|
|
|
{ |
|
96
|
|
|
return \Carbon\Carbon::parse($value)->format('d M Y'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return the formatted number of users in this group |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getUsersCountAttribute($value) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
return $this->users->count() . ' users'; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|