ldc-boutique-cnc-sable/app/Models/Faq.php

50 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
2026-06-14 21:32:07 +02:00
declare(strict_types=1);
namespace App\Models;
use App\Interfaces\ModelWithOrder;
use App\Traits\Models\HasOrder;
2026-06-14 21:32:07 +02:00
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Model;
2026-06-14 21:32:07 +02:00
/**
* @property int $id
* @property string $question
* @property string $answer
* @property int $order
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
*
* @mixin \Eloquent
*/
#[Fillable(['question', 'answer'])]
#[Guarded([
'order',
])]
class Faq extends Model implements ModelWithOrder
{
use HasOrder;
2026-06-14 21:32:07 +02:00
#[\Override]
protected static function booted()
{
static::creating(function (self $model): void {
self::manageFields($model);
});
static::updating(function (self $model): void {
self::manageFields($model);
});
}
2026-06-14 21:32:07 +02:00
private static function manageFields(self $model): void
{
if (is_null($model->order)) {
$maxOrder = self::query()->max('order');
$model->order = is_numeric($maxOrder) ? (int) $maxOrder + 1 : 0;
2026-06-14 21:32:07 +02:00
}
}
}