69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\BlockTypeEnum;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
/**
|
|
* Represents a content block belonging to a page section.
|
|
*
|
|
* @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);
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::updating(function (self $block) {
|
|
$picture = request()->file('data.picture.value');
|
|
|
|
if ($picture instanceof UploadedFile) {
|
|
$data = $block->data;
|
|
$data->picture->value = FileStorageHelper::storeFile($block, $picture, true);
|
|
$block->data = $data;
|
|
}
|
|
});
|
|
}
|
|
}
|