155 lines
4.1 KiB
PHP
155 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Stancl\Tenancy\Contracts\Tenant as TenantContract;
|
|
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
|
|
|
class Tenant extends BaseTenant implements TenantContract
|
|
{
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
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);
|
|
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::resetCache();
|
|
});
|
|
}
|
|
|
|
// * 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::setTenantsCached();
|
|
}
|
|
|
|
/**
|
|
* Set all tenants in cache.
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public static function setTenantsCached(): \Illuminate\Support\Collection
|
|
{
|
|
return Cache::rememberForever('tenantsGlobal', 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);
|
|
}
|
|
}
|