belongsTo(Page::class, 'parent_id'); } public function children(): HasMany { return $this->hasMany(Page::class, 'parent_id'); } public function sections(): MorphToMany { return $this->objects()->whereHas('type.parent', function($query) { $query->where(['name' => 'page-section']); }); } public function sidebar(): Model { return $this->getObject('page-sidebar'); } public function sidebars(): MorphToMany { return $this->objects()->whereHas('type', function($query) { $query->where(['name' => 'page-sidebar']); }); } public function getLinkAttribute(): string { return $this->parents->reverse()->push($this)->pluck('slug')->implode('/'); } public function getParentsAttribute(): Collection { $page = $this; $result = collect([]); while($page = $page->parent) $result->push($page); return $result; } public function getParsedTypeAttribute(): array { return ['name' => $this->type, 'title' => PageType::TITLES[$this->type] ?? null]; } public function addSection($typeName, $ord) { $this->createObject($typeName, $ord); } public static function root() { return self::query()->where(['parent_id' => 0])->get(); } }