*/ protected $fillable = [ 'type', 'seo_title', 'seo_description', 'title', 'content', 'tenant_id', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'type' => StaticPageType::class, ]; // * EVENTS /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { // * ASSERTIONS self::assertTitleIsUnique($model); // * CHANGES $model->slug = Str::slug($model->title); }); static::updating(function (self $model) { // * ASSERTIONS self::assertTitleIsUnique($model); // * CHANGES $model->slug = Str::slug($model->title); }); } // * ROUTABLE /** * Get the value of the model's route key. * * @return string|integer */ public function getRouteKey(): string|int { /** @var \App\Enums\StaticPageType $enum */ $enum = $this->getAttribute($this->getRouteKeyName()); return $enum->value(); } /** * Get the route key for the model. * * ! Type is the routable parameter * * @return string */ public function getRouteKeyName(): string { return 'type'; } // * METHODS /** * Asserts using validation that the title is unique * * @param self $model * @return void * @throws \Illuminate\Validation\ValidationException If a static page title already exists. */ private static function assertTitleIsUnique(self $model): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique( $table, 'title', $model->title, $model->getKey(), function (Builder $query) use ($model) { $query->where('tenant_id', $model->tenant->getKey()); } ); ToolboxHelper::assertFieldIsUnique( $table, 'slug', Str::slug($model->title), $model->getKey(), function (Builder $query) use ($model) { $query->where('tenant_id', $model->tenant->getKey()); } ); } /** * Set default settings for a specific tenant. * * @param \App\Models\Tenant $tenant * @return void */ public function setDefaultPages(Tenant $tenant): void { collect([StaticPageType::confidentiality, StaticPageType::legals, StaticPageType::rules]) ->map(function (StaticPageType $enum) use ($tenant) { $staticPageModel = new StaticPage(); $staticPageModel->seo_title = sprintf('Seo title pour la page de %s', $enum->label()); $staticPageModel->seo_description = sprintf( 'Seo description pour la page de %s', $enum->label() ); $staticPageModel->title = sprintf('Page de %s', $enum->label()); $staticPageModel->type = $enum; $staticPageModel->content = 'Ceci est le contenu de la page.'; $staticPageModel->tenant_id = $tenant->getkey(); $staticPageModel->saveOrFail(); }); } // * RELATIONS /** * Activities logs One-to-many relationship. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(ActivityLog::class); } }