*/ #[\Override] protected $fillable = [ 'seo_title', 'seo_description', 'title', 'content', ]; /** * The attributes that should be cast. * * @var array */ #[\Override] protected $casts = [ 'type' => StaticPageType::class, ]; // * EVENTS /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $model): void { // * ASSERTIONS self::assertTitleIsUnique($model->title); // * CHANGES $model->slug = Str::slug($model->title); }); static::updating(function (self $model): void { // * ASSERTIONS self::assertTitleIsUnique($model->title, $model->id); // * CHANGES $model->slug = Str::slug($model->title); }); } // * ROUTABLE /** * Get the value of the model's route key. * * @return mixed */ public function getRouteKey() { /** @var StaticPageType */ $enum = $this->getAttribute($this->getRouteKeyName()); return $enum->value(); } /** * Get the route key for the model. * * ! Type is the routable parameter */ public function getRouteKeyName(): string { return 'type'; } // * METHODS /** * Asserts using validation that the title is unique * * * @throws ValidationException If an actu title already exists. */ private static function assertTitleIsUnique(?string $title, ?int $id = null): void { $table = new self()->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id); } // * RELATIONS /** * Activities logs One-to-many relationship. * * @return HasMany */ public function activityLogs(): HasMany { return $this->hasMany(ActivityLog::class); } }