Passed
Push — master ( f1a782...17ae54 )
by Ion
04:19 queued 45s
created

Role::permissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.9666
1
<?php
2
3
namespace App\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
/**
12
 * Class Role
13
 *
14
 * @property int $id
15
 * @property string $name
16
 * @property Carbon|null $created_at
17
 * @property Carbon|null $updated_at
18
 * @property Carbon|null $deleted_at
19
 *
20
 * @property-read Collection|User[] $users
21
 * @property-read Collection|Permission[] $permissions
22
 * @property-read Collection|RolePermission[] $rolePermissions
23
 *
24
 * @package App\Models
25
 */
26
class Role extends Model
27
{
28
    use SoftDeletes;
29
30
    /** @var int */
31
    const ID_ADMIN = 1;
32
33
    /** @var int */
34
    const ID_USER = 2;
35
36
    /** @var bool */
37
    public $timestamps = true;
38
39
    /** @var string */
40
    protected $table = 'roles';
41
42
    /** @var array */
43
    protected $fillable = [
44
        'name'
45
    ];
46
47
    /** @var array */
48
    protected $visible = [
49
        'id',
50
        'name',
51
        'users',
52
        'permissions',
53
        'access',
54
        'rolePermissions'
55
    ];
56
57
    /** @var array */
58
    protected $sortable = [
59
        'id',
60
        'name'
61
    ];
62
63
    /** @var array */
64
    protected $searchable = [
65
        'name'
66
    ];
67
68
    /**
69
     * Role users.
70
     *
71
     * @return HasMany
72
     */
73
    public function users()
74
    {
75
        return $this->hasMany(User::class, 'role_id', 'id');
76
    }
77
78
    /**
79
     * @return BelongsToMany
80
     */
81
    public function permissions()
82
    {
83
        return $this->belongsToMany(Permission::class, 'role_permissions', 'role_id', 'permission_id')
84
            ->as('access')
85
            ->using(RolePermission::class)
86
            ->withPivot([
87
                'read',
88
                'create',
89
                'update',
90
                'delete',
91
                'manage'
92
            ]);
93
    }
94
95
    /**
96
     * Role permissions.
97
     *
98
     * @return HasMany
99
     */
100
    public function rolePermissions()
101
    {
102
        return $this->hasMany(RolePermission::class, 'role_id', 'id');
103
    }
104
}
105