ldc-legaulois/app/Models/Block.php

70 lines
1.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
use App\Enums\BlockTypeEnum;
2026-06-11 10:47:10 +02:00
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2026-09-25 16:25:31 +02:00
use Illuminate\Http\UploadedFile;
/**
* Represents a content block belonging to a page section.
*
2026-09-25 16:25:31 +02:00
* @property int $id Primary key.
* @property int $section_id Foreign key referencing the parent section.
* @property BlockTypeEnum $type The type of the block.
* @property object|null $data JSON data associated with the block.
* @property int $order Display order within the section.
* @property \Illuminate\Support\Carbon $created_at Timestamp when the block was created.
* @property \Illuminate\Support\Carbon $updated_at Timestamp when the block was last updated.
* @property-read SectionPage $section The parent section this block belongs to.
*/
class Block extends Model
{
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'section_id',
'type',
'data',
'order',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => BlockTypeEnum::class,
'data' => 'object',
];
/**
* Get the section that owns this block.
*
* @return BelongsTo<SectionPage, Block>
*/
public function section(): BelongsTo
{
return $this->belongsTo(SectionPage::class);
}
2026-06-11 10:47:10 +02:00
protected static function booted(): void
{
static::updating(function (self $block) {
2026-09-25 16:25:31 +02:00
$picture = request()->file('data.picture.value');
if ($picture instanceof UploadedFile) {
$data = $block->data;
2026-06-11 10:47:10 +02:00
$data->picture->value = FileStorageHelper::storeFile($block, $picture, true);
2026-09-25 16:25:31 +02:00
$block->data = $data;
2026-06-11 10:47:10 +02:00
}
});
}
}