PageSection.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Models;
  3. /*
  4. * PageSection — одна секция страницы в page-builder.
  5. *
  6. * Два типа (поле type):
  7. * 'block' — ссылка на переиспользуемый блок (block_id → blocks)
  8. * 'content' — произвольный HTML-контент (поле content)
  9. *
  10. * Порядок вывода на странице задаётся sort_order.
  11. */
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. class PageSection extends Model
  15. {
  16. protected $fillable = ['page_id', 'type', 'content', 'block_id', 'sort_order'];
  17. public function page(): BelongsTo
  18. {
  19. return $this->belongsTo(Page::class);
  20. }
  21. public function block(): BelongsTo
  22. {
  23. return $this->belongsTo(Block::class);
  24. }
  25. // Возвращает HTML для рендера на публичной странице
  26. public function getRenderedContent(): string
  27. {
  28. if ($this->type === 'content') {
  29. return (string) $this->content;
  30. }
  31. if ($this->block && $this->block->is_active) {
  32. return $this->block->render();
  33. }
  34. return '';
  35. }
  36. }