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

Permission::roles()   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 Permission
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|Role[] $roles
21
 * @property-read Collection|RolePermission[] $rolePermissions
22
 *
23
 * @package App\Models
24
 */
25
class Permission extends Model
26
{
27
    use SoftDeletes;
28
29
    /** @var int */
30
    const ID_USERS = 1;
31
32
    /** @var int */
33
    const ID_ROLES = 2;
34
35
    /** @var int */
36
    const ID_TASKS = 3;
37
38
    /** @var bool */
39
    public $timestamps = true;
40
41
    /** @var string */
42
    protected $table = 'permissions';
43
44
    /** @var array */
45
    protected $fillable = [
46
        'name'
47
    ];
48
49
    /** @var array */
50
    protected $visible = [
51
        'id',
52
        'name',
53
        'roles',
54
        'access',
55
        'rolePermissions'
56
    ];
57
58
    /** @var array */
59
    protected $sortable = [
60
        'id',
61
        'name'
62
    ];
63
64
    /** @var array */
65
    protected $searchable = [
66
        'name'
67
    ];
68
69
    /**
70
     * @return BelongsToMany
71
     */
72
    public function roles()
73
    {
74
        return $this->belongsToMany(Role::class, 'role_permissions', 'permission_id', 'role_id')
75
            ->as('access')
76
            ->using(RolePermission::class)
77
            ->withPivot([
78
                'read',
79
                'create',
80
                'update',
81
                'delete',
82
                'manage'
83
            ]);
84
    }
85
86
    /**
87
     * Role permissions.
88
     *
89
     * @return HasMany
90
     */
91
    public function rolePermissions()
92
    {
93
        return $this->hasMany(RolePermission::class, 'permission_id', 'id');
94
    }
95
}
96