belongsTo(Menu::class, 'parent_id'); } protected function urlParameter(): Attribute { return Attribute::make( get: function (?string $value, ?array $attributes) { $parameter = null; switch ($attributes['type']) { case 'single-link': $parameter = $attributes['parameter']; break; default: $model = config('constants.menu_type_options')[$attributes['type']] ?? null; if ($model) { $modelClass = "Modules\\CCMS\\Models\\$model"; $parameter = optional($modelClass::find($attributes['parameter']))->slug; } break; } return $parameter; } ); } public function children() { return $this->hasMany(Menu::class, 'parent_id'); } public function location(): Attribute { return Attribute::make( get: function (mixed $value, array $attributes) { return config('constants.menu_location_options')[$attributes['menu_location_id']]; } ); } public function hasSubMenu() { return $this->relationLoaded('children') ? $this->children->isNotEmpty() : $this->children()->exists(); } protected function routeName(): Attribute { return Attribute::make( get: function (mixed $value, array $attributes) { $route = null; switch ($attributes['type']) { case 'pages': $route = route('page.load', Page::whereId($attributes['parameter'])->value('slug')); break; case 'services': $route = route('service.single', Service::whereId($attributes['parameter'])->value('slug')); break; case 'countries': $route = route('country.single', Country::whereId($attributes['parameter'])->value('slug')); break; case 'blogs': $route = route('blog.single', Blog::whereId($attributes['parameter'])->value('slug')); break; default: $route = ($attributes['parameter'] == '#') ? 'javascript:void(0)' : (preg_match('/^https/', $attributes['parameter']) ? $attributes['parameter'] : config('app.url') . $attributes['parameter']); } return $route; } ); } protected function image(): Attribute { return Attribute::make( get: fn($value) => asset($value), ); } }