-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMenuItem.php
More file actions
100 lines (81 loc) · 2.5 KB
/
MenuItem.php
File metadata and controls
100 lines (81 loc) · 2.5 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
class MenuItem extends Model implements Sortable
{
use SortableTrait;
protected $table = 'menu_items';
protected $fillable = [
'menu_section_id',
'label',
'label_key',
'label_overrides',
'url',
'route_name',
'route_params',
'open_in_new_tab',
'is_active',
'sort_order',
];
protected $casts = [
'label_overrides' => 'array',
'route_params' => 'array',
'open_in_new_tab' => 'bool',
'is_active' => 'bool',
'sort_order' => 'int',
];
public $sortable = [
'order_column_name' => 'sort_order',
'sort_when_creating' => true,
'sort_on_has_many' => true,
];
public function section(): BelongsTo
{
return $this->belongsTo(MenuSection::class, 'menu_section_id');
}
public function resolvedLabel(?string $locale = null): string
{
$locale = $locale ?: app()->getLocale();
$override = $this->label_overrides[$locale] ?? null;
if (is_string($override) && trim($override) !== '') {
return $override;
}
if (is_string($this->label_key) && $this->label_key !== '') {
return __($this->label_key);
}
return (string) ($this->label ?? '');
}
public function resolvedHref(): ?string
{
if (is_string($this->route_name) && $this->route_name !== '') {
$params = is_array($this->route_params) ? $this->route_params : [];
if (Route::has($this->route_name)) {
return route($this->route_name, $params);
}
}
if (is_string($this->url) && $this->url !== '') {
return $this->url;
}
return null;
}
protected static function booted(): void
{
static::saved(function (self $item) {
$location = $item->section?->location;
if ($location) {
Cache::forget("menus.location.{$location}.v1");
}
});
static::deleted(function (self $item) {
$location = $item->section?->location;
if ($location) {
Cache::forget("menus.location.{$location}.v1");
}
});
}
}