|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models\enso\Menus; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use LaravelEnso\Menus\App\Exceptions\Menu as Exception; |
|
9
|
|
|
use App\Models\enso\Permissions\Permission; |
|
10
|
|
|
use App\Models\enso\Roles\Role; |
|
11
|
|
|
use LaravelEnso\Tables\App\Traits\TableCache; |
|
12
|
|
|
/** |
|
13
|
|
|
* @property int $id |
|
14
|
|
|
* @property int $parent_id |
|
15
|
|
|
* @property int $permission_id |
|
16
|
|
|
* @property string $name |
|
17
|
|
|
* @property string $icon |
|
18
|
|
|
* @property int $order_index |
|
19
|
|
|
* @property boolean $has_children |
|
20
|
|
|
* @property string $created_at |
|
21
|
|
|
* @property string $updated_at |
|
22
|
|
|
* @property Menu $menu |
|
23
|
|
|
* @property Permission $permission |
|
24
|
|
|
* @property Role[] $roles |
|
25
|
|
|
*/ |
|
26
|
|
|
class Menu extends Model |
|
27
|
|
|
{ |
|
28
|
|
|
use TableCache; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The table associated with the model. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $table = 'menus'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fillable = ['parent_id', 'permission_id', 'name', 'icon', 'order_index', 'has_children', 'created_at', 'updated_at']; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
protected $casts = [ |
|
45
|
|
|
'has_children' => 'boolean', 'parent_id' => 'integer', 'permission_id' => 'integer', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
public function parent() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->belongsTo(self::class); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function children() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->hasMany(self::class, 'parent_id', 'id'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function permission() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->belongsTo(Permission::class); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function rolesWhereIsDefault() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->hasMany(Role::class); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getComputedIconAttribute() |
|
69
|
|
|
{ |
|
70
|
|
|
return Str::contains($this->icon, ' ') |
|
71
|
|
|
? explode(' ', $this->icon) |
|
72
|
|
|
: $this->icon; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function delete() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->children()->exists()) { |
|
78
|
|
|
throw Exception::hasChildren(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($this->rolesWhereIsDefault()->exists()) { |
|
82
|
|
|
throw Exception::usedAsDefault(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
parent::delete(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function scopeIsParent(Builder $query) |
|
89
|
|
|
{ |
|
90
|
|
|
return $query->whereHasChildren(true); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function scopeIsNotParent(Builder $query) |
|
94
|
|
|
{ |
|
95
|
|
|
return $query->whereHasChildren(false); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|