82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\HasActivityLog;
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* FaqTheme.
|
|
*
|
|
* @property string $question Question
|
|
* @property string $answer Answer
|
|
* @property-read Carbon $created_at Creation Date.
|
|
* @property-read Carbon $updated_at Last update Date.
|
|
* @property-read Collection|FaqQuestion[] $questions
|
|
* Associated questions.
|
|
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
|
|
*/
|
|
class FaqTheme extends Model
|
|
{
|
|
use HasActivityLog;
|
|
use HasFactory;
|
|
use IsCachedAsCollection;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
#[\Override]
|
|
protected $fillable = ['name'];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::created(fn () => self::removeCached());
|
|
static::updated(fn () => self::removeCached());
|
|
static::deleting(function (self $model): void {
|
|
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 HasMany<FaqQuestion, $this>
|
|
*/
|
|
public function questions(): HasMany
|
|
{
|
|
return $this->hasMany(FaqQuestion::class, 'theme_id');
|
|
}
|
|
|
|
/**
|
|
* Activities logs One-to-many relationship.
|
|
*
|
|
* @return HasMany<ActivityLog, $this>
|
|
*/
|
|
public function activityLogs(): HasMany
|
|
{
|
|
return $this->hasMany(ActivityLog::class);
|
|
}
|
|
}
|