*/ protected $fillable = ['id', 'theme']; /** * Define the custom columns (that won't be stored in the data JSON column) * by overriding the getCustomColumns() method. * * @see https://tenancyforlaravel.com/docs/v3/tenants * * @return array */ public static function getCustomColumns(): array { return [ 'id', 'theme', ]; } /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::assertThemeIsUnique($model->theme); $model->id = Str::slug($model->theme); }); static::created(function (self $model) { (new Settings())->setDefaultSettings($model); (new StaticPage())->setDefaultPages($model); self::resetCache(); }); static::updating(function (self $model) { self::assertThemeIsUnique($model->theme, $model->getKey()); $model->id = Str::slug($model->theme); }); static::updated(function () { self::resetCache(); }); static::deleted(function () { self::resetCache(); }); } // * METHODS /** * Asserts using validation that the theme is unique * * @param string|null $theme * @param string|null $id * @return void * @throws \Illuminate\Validation\ValidationException If theme already exists. */ private static function assertThemeIsUnique(string|null $theme, ?string $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'theme', $theme, $id); ToolboxHelper::assertFieldIsUnique($table, 'id', Str::slug($theme), $id); } /** * Reset all cache elements related to this model. * * @return void */ private static function resetCache(): void { Cache::forget('tenantsGlobal'); self::setTenantsCached(); } /** * Set all tenants in cache. * * @return \Illuminate\Support\Collection */ public static function setTenantsCached(): \Illuminate\Support\Collection { return Cache::remember('tenantsGlobal', 86400, function () { return Tenant::get(); }); } // * RELATIONS /** * Has One Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function setting(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(Settings::class); } }