1 | <?php |
||
2 | |||
3 | namespace Ultraware\Roles\Traits; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Collection; |
||
6 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
7 | use Ultraware\Roles\Models\Permission; |
||
8 | |||
9 | trait RoleHasRelations |
||
10 | { |
||
11 | /** |
||
12 | * Role belongs to many permissions. |
||
13 | * |
||
14 | * @return BelongsToMany |
||
15 | */ |
||
16 | public function permissions() |
||
17 | { |
||
18 | return $this->belongsToMany(config('roles.models.permission'))->withTimestamps(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Role belongs to many users. |
||
23 | * |
||
24 | * @return BelongsToMany |
||
25 | */ |
||
26 | public function users() |
||
27 | { |
||
28 | return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps(); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Attach permission to a role. |
||
33 | * |
||
34 | * @param int|Permission $permission |
||
35 | * @return int|bool |
||
36 | */ |
||
37 | public function attachPermission($permission) |
||
38 | { |
||
39 | return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Detach permission from a role. |
||
44 | * |
||
45 | * @param int|Permission $permission |
||
46 | * @return int |
||
47 | */ |
||
48 | public function detachPermission($permission) |
||
49 | { |
||
50 | return $this->permissions()->detach($permission); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Detach all permissions. |
||
55 | * |
||
56 | * @return int |
||
57 | */ |
||
58 | public function detachAllPermissions() |
||
59 | { |
||
60 | return $this->permissions()->detach(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Sync permissions for a role. |
||
65 | * |
||
66 | * @param array|Permission[]|Collection $permissions |
||
67 | * @return array |
||
68 | */ |
||
69 | public function syncPermissions($permissions) |
||
70 | { |
||
71 | return $this->permissions()->sync($permissions); |
||
72 | } |
||
73 | } |
||
74 |