Page.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Models;
  3. /*
  4. * Page — статическая страница сайта.
  5. *
  6. * Таблица: pages | slug — уникальный ключ ('home', 'services', 'contacts')
  7. * Метод findBySlug() — удобный поиск с 404 при отсутствии
  8. */
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. class Page extends Model
  13. {
  14. protected $fillable = ['slug', 'title', 'content', 'meta_title', 'meta_description', 'is_active'];
  15. protected $casts = ['is_active' => 'boolean'];
  16. public static function findBySlug(string $slug): self
  17. {
  18. return static::where('slug', $slug)->firstOrFail();
  19. }
  20. // Секции страницы (сводная таблица page_sections с порядком)
  21. public function pageSections(): HasMany
  22. {
  23. return $this->hasMany(PageSection::class)->orderBy('sort_order');
  24. }
  25. // Блоки страницы через pivot — удобно для attach/detach/sync
  26. public function blocks(): BelongsToMany
  27. {
  28. return $this->belongsToMany(Block::class, 'page_sections')
  29. ->withPivot('sort_order')
  30. ->orderByPivot('sort_order');
  31. }
  32. }