ldc-legaulois/app/Models/SectionPage.php
2026-09-25 16:25:31 +02:00

99 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\StaticPageType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Validation\ValidationException;
/**
* SectionPage
*
* Represents a section belonging to a static page.
*
* @property int $id
* @property int $page_id
* @property string $name
* @property bool $visibility
* @property object $data
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class SectionPage extends Model
{
/**
* The table associated with the model.
*
* @var string|null
*/
protected $table = 'section_pages';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'page_id',
'name',
'visibility',
'data',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'visibility' => 'boolean',
'data' => 'object',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::saving(function ($section) {
$section->load('page');
if (
$section->page->type === StaticPageType::raiseSustainably
&& $section->name === 'section 3'
&& ! str_contains($section->data->title->value, ':nbrKeyList')
) {
throw ValidationException::withMessages([
'data.title.value' => __('validation.custom.data_title_value.contains_placeholder'),
]);
}
});
}
/**
* Returns the static page this section belongs to.
*/
public function page(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
/**
* Returns all blocks associated with this section.
*/
public function blocks(): HasMany
{
return $this->hasMany(Block::class, 'section_id');
}
/**
* Returns all pictos associated with this section, ordered by their position.
*/
public function pictos(): HasMany
{
return $this->hasMany(Picto::class, 'section_id')->orderBy('order');
}
}