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

49 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Interfaces\ModelWithOrder;
use App\Traits\Models\HasOrder;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Model;
/**
* @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;
#[\Override]
protected static function booted()
{
static::creating(function (self $model): void {
self::manageFields($model);
});
static::updating(function (self $model): void {
self::manageFields($model);
});
}
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;
}
}
}