| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace App\Models;
- /*
- * Page — статическая страница сайта.
- *
- * Таблица: pages | slug — уникальный ключ ('home', 'services', 'contacts')
- * Метод findBySlug() — удобный поиск с 404 при отсутствии
- */
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- class Page extends Model
- {
- protected $fillable = ['slug', 'title', 'content', 'meta_title', 'meta_description', 'is_active'];
- protected $casts = ['is_active' => 'boolean'];
- public static function findBySlug(string $slug): self
- {
- return static::where('slug', $slug)->firstOrFail();
- }
- // Секции страницы (сводная таблица page_sections с порядком)
- public function pageSections(): HasMany
- {
- return $this->hasMany(PageSection::class)->orderBy('sort_order');
- }
- // Блоки страницы через pivot — удобно для attach/detach/sync
- public function blocks(): BelongsToMany
- {
- return $this->belongsToMany(Block::class, 'page_sections')
- ->withPivot('sort_order')
- ->orderByPivot('sort_order');
- }
- }
|