summaryrefslogtreecommitdiff
path: root/src/Models/Auth/Role.php
blob: bba98694f30520b20cfed8983ae727f8e12b9b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

namespace Engelsystem\Models\Auth;

use Carbon\Carbon;
use Engelsystem\Models\BaseModel;
use Engelsystem\Models\Team;
use Engelsystem\Models\User\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;

/**
 * @property integer                                   $id
 * @property string                                    $name
 * @property string|null                               $description
 * @property Carbon                                    $created_at
 * @property Carbon                                    $updated_at
 *
 * @property-read QueryBuilder|Collection|Permission[] $permissions
 * @property-read QueryBuilder|Collection|Team[]       $teams
 * @property-read QueryBuilder|Collection|User[]       $users
 *
 * @method static QueryBuilder|Role              whereId($value)
 * @method static QueryBuilder|Collection|Role[] whereName($value)
 * @method static QueryBuilder|Collection|Role[] whereDescription($value)
 * @method static QueryBuilder|Collection|Role[] whereCreatedAt($value)
 * @method static QueryBuilder|Collection|Role[] whereUpdatedAt($value)
 */
class Role extends BaseModel
{
    /** @var bool enable timestamps */
    public $timestamps = true;

    /** The attributes that are mass assignable */
    protected $fillable = [
        'name',
        'description',
    ];

    /**
     * The roles permissions
     *
     * @return BelongsToMany
     */
    public function permissions()
    {
        return $this
            ->belongsToMany(Permission::class)
            ->withTimestamps();
    }

    /**
     * The teams that that have the role assigned
     *
     * @return BelongsToMany
     */
    public function teams()
    {
        return $this
            ->belongsToMany(Team::class)
            ->withTimestamps();
    }

    /**
     * The users that that have the role assigned
     *
     * @return HasManyDeep
     */
    public function users()
    {
        return $this->hasManyDeepFromRelations($this->teams(), (new Team)->users());
    }
}