| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Models;
- /*
- * Block — блок контента с привязанным макетом и структурированными полями.
- *
- * Таблица: blocks
- * layout — ключ макета из BlockLayoutRegistry (why_us, hero_banner и т.д.)
- * data — значения полей в виде массива (хранится как JSON)
- *
- * getByName() — получить активный блок по системному имени
- * render() — рендерит блок через Blade-шаблон blocks/{layout}.blade.php
- */
- use App\Support\BlockLayoutRegistry;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- class Block extends Model
- {
- protected $fillable = ['name', 'title', 'layout', 'content', 'data', 'is_active'];
- protected $casts = [
- 'is_active' => 'boolean',
- 'data' => 'array',
- ];
- public static function getByName(string $name): ?self
- {
- return static::where('name', $name)->where('is_active', true)->first();
- }
- // Рендерит блок через соответствующий Blade-шаблон, возвращает готовый HTML
- public function render(): string
- {
- if (!$this->layout || !$this->data) {
- return '';
- }
- $view = 'blocks.' . $this->layout;
- if (!view()->exists($view)) {
- return '';
- }
- return view($view, ['data' => $this->data])->render();
- }
- // Определение макета из реестра (null если макет не задан или не найден)
- public function layoutDefinition(): ?array
- {
- return $this->layout ? BlockLayoutRegistry::get($this->layout) : null;
- }
- // Страницы, на которых размещён этот блок
- public function pages(): BelongsToMany
- {
- return $this->belongsToMany(Page::class, 'page_sections')->withPivot('sort_order');
- }
- }
|