89 lines
2.1 KiB
PHP
89 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Pages;
|
|
|
|
use App\Support\HasObjectsTrait;
|
|
use App\Support\RelationValuesTrait;
|
|
use App\Support\UuidScopeTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class Page extends Model {
|
|
use UuidScopeTrait, SoftDeletes, HasObjectsTrait, RelationValuesTrait;
|
|
|
|
protected $dates = [
|
|
];
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'parent_id',
|
|
'slug',
|
|
'type',
|
|
'name',
|
|
'title',
|
|
'h1',
|
|
'ord'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'id'
|
|
];
|
|
|
|
|
|
public function parent(): BelongsTo {
|
|
return $this->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();
|
|
}
|
|
|
|
}
|