*/ protected $fillable = ['id', 'name']; /** * 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', 'name', ]; } /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::assertNameIsUnique($model->name); $model->id = Str::slug($model->name); }); static::created(function (self $model) { (new Setting())->setDefaultSettings($model); (new Formulary())->setDefaultFormularies($model); (new Directory())->setDefaultDirectories($model); self::resetCache(); }); static::updating(function (self $model) { self::assertNameIsUnique($model->name, $model->getKey()); $model->id = Str::slug($model->name); }); static::updated(function () { self::resetCache(); }); static::deleted(function (self $model) { self::resetCache(); /** @var \App\Models\User $userModel */ $userModel = auth('backend')->user(); dispatch(new SendTenantDeletedMail( sprintf('%s %s', $userModel->first_name, $userModel->last_name), $model->name )); }); } // * METHODS /** * Asserts using validation that the name is unique * * @param string|null $name * @param string|null $id * @return void * @throws \Illuminate\Validation\ValidationException If name already exists. */ private static function assertNameIsUnique(string|null $name, ?string $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id); ToolboxHelper::assertFieldIsUnique($table, 'id', Str::slug($name), $id); } // * RELATIONSHIPS /** * Reset all cache elements related to this model. * * @return void */ private static function resetCache(): void { Cache::forget('tenantsGlobal'); self::getTenantsCached(); } /** * Get all tenants in cache. * * @return \Illuminate\Support\Collection */ public static function getTenantsCached(): \Illuminate\Support\Collection { return Cache::remember('tenantsGlobal', 86400, function () { return Tenant::get(); }); } /** * Has Many Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function appUsers(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(AppUser::class); } /** * Has Many Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function directories(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Directory::class); } /** * Has Many Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function documents(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Document::class); } /** * Has Many Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function formularies(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Formulary::class); } /** * Has Many Relationship Method. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function settings(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Setting::class); } }