69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* FaqTheme.
|
|
*
|
|
* @property string $question Question
|
|
* @property string $answer Answer
|
|
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\FaqQuestion[] $questions
|
|
* Associated questions.
|
|
*/
|
|
class FaqTheme extends Model
|
|
{
|
|
use HasFactory;
|
|
use IsCachedAsCollection;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<string>
|
|
*/
|
|
protected $fillable = ['name'];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::created(fn () => self::removeCached());
|
|
static::updated(fn () => self::removeCached());
|
|
static::deleting(function (self $model) {
|
|
if ($model->questions()->count()) {
|
|
throw ValidationException::withMessages([
|
|
'error' =>
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
trans("Il n'est pas possible de supprimer ce theme car il est associé a des questions,\u{00A0} suppirmer les questions d'abord.")
|
|
]);
|
|
}
|
|
});
|
|
static::deleted(fn () => self::removeCached());
|
|
}
|
|
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Question relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function questions(): HasMany
|
|
{
|
|
return $this->hasMany(FaqQuestion::class, 'theme_id');
|
|
}
|
|
}
|