Permission::roles()   A
last analyzed

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\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
use Illuminate\Database\Query\Builder as QueryBuilder;
12
use IonGhitun\MysqlEncryption\Models\BaseModel;
13
14
/**
15
 * Class Permission
16
 *
17
 * @property int $id
18
 * @property string $name
19
 * @property Carbon|null $created_at
20
 * @property Carbon|null $updated_at
21
 * @property Carbon|null $deleted_at
22
 *
23
 * @property-read Collection|Role[] $roles
24
 * @property-read Collection|RolePermission[] $rolePermissions
25
 * @property-read int|null $role_permissions_count
26
 * @property-read int|null $roles_count
27
 *
28
 * @method static Builder|Permission newModelQuery()
29
 * @method static Builder|Permission newQuery()
30
 * @method static Builder|Permission query()
31
 * @method static Builder|Permission whereCreatedAt($value)
32
 * @method static Builder|Permission whereDeletedAt($value)
33
 * @method static Builder|Permission whereId($value)
34
 * @method static Builder|Permission whereName($value)
35
 * @method static Builder|Permission whereUpdatedAt($value)
36
 * @method static QueryBuilder|Permission onlyTrashed()
37
 * @method static QueryBuilder|Permission withTrashed()
38
 * @method static QueryBuilder|Permission withoutTrashed()
39
 * @method static Builder|BaseModel orWhereEncrypted($column, $value)
40
 * @method static Builder|BaseModel orWhereNotEncrypted($column, $value)
41
 * @method static Builder|BaseModel orderByEncrypted($column, $direction)
42
 * @method static Builder|BaseModel whereEncrypted($column, $value)
43
 * @method static Builder|BaseModel whereNotEncrypted($column, $value)
44
 *
45
 * @package App\Models
46
 */
47
class Permission extends Model
48
{
49
    use SoftDeletes;
50
51
    /** @var int */
52
    const ID_USERS = 1;
53
54
    /** @var int */
55
    const ID_ROLES = 2;
56
57
    /** @var int */
58
    const ID_TASKS = 3;
59
60
    /** @var bool */
61
    public $timestamps = true;
62
63
    /** @var string */
64
    protected $table = 'permissions';
65
66
    /** @var array */
67
    protected $fillable = [
68
        'name'
69
    ];
70
71
    /** @var array */
72
    protected $visible = [
73
        'id',
74
        'name',
75
        'roles',
76
        'access',
77
        'rolePermissions'
78
    ];
79
80
    /** @var array */
81
    protected $sortable = [
82
        'id',
83
        'name'
84
    ];
85
86
    /** @var array */
87
    protected $searchable = [
88
        'name'
89
    ];
90
91
    /**
92
     * @return BelongsToMany
93
     */
94
    public function roles()
95
    {
96
        return $this->belongsToMany(Role::class, 'role_permissions', 'permission_id', 'role_id')
97
                    ->as('access')
98
                    ->using(RolePermission::class)
99
                    ->withPivot([
100
                        'read',
101
                        'create',
102
                        'update',
103
                        'delete',
104
                        'manage'
105
                    ]);
106
    }
107
108
    /**
109
     * Role permissions.
110
     *
111
     * @return HasMany
112
     */
113
    public function rolePermissions()
114
    {
115
        return $this->hasMany(RolePermission::class, 'permission_id', 'id');
116
    }
117
}
118