Menu.php
2.21 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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
use Dyrynda\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletes; //1
use Route;
class Menu extends Model implements Auditable //2
{
use SoftDeletes, CascadeSoftDeletes;
use \OwenIt\Auditing\Auditable; //3
protected $cascadeDeletes = ['aksesmenu'];
protected $dates = ['deleted_at'];
protected $fillable = [
'nama', 'kode', 'link', 'icon', 'tampilkan', 'private', 'parent_id', 'perbaikan', 'pengumuman'
];
public function child() // One level child
{
return $this->hasMany('App\Models\Menu', 'parent_id');
}
public function children() // Recursive children
{
return $this->child()->with('children');
}
public function parent() // One level parent
{
return $this->belongsTo('App\Models\Menu', 'parent_id');
}
public function parents() // Recursive parents
{
return $this->parent()->with('parents');
}
public function aksesmenu()
{
return $this->hasMany('App\Models\Aksesmenu');
}
public function active($child, $path = null)
{
// $active = 'active open';
$class = 'active';
$current = explode("/", $path);
$active = $this->kode == $current[0] ? $class : '';
if ($this->kode == 'home' && $current[0] == '') {
$active = $class;
}
if (isset($child[$this->id])) {
foreach ($child[$this->id] as $child_item) {
if ($child_item->kode == $current[0]) {
$active = $class ; break;
}
}
}
return $active;
}
public function generate($menu, $submenu)
{
return [
'nama' => $menu->nama,
'url' => NULL,
'route' => NULL,
'icon' => 'fa '.$menu->icon,
'submenu' => $submenu
];
}
public function setNamaAttribute($value)
{
$this->attributes['nama'] = ucwords(trim($value));
}
}