1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 14:34:04 +00:00
tenancy/src/Database/Models/Tenant.php
lukinovec 2201545c51
Update currentOrFail declaration
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
2022-10-11 07:55:55 +02:00

75 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Database\TenantCollection;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
/**
* @property string|int $id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property array $data
*
* @method static TenantCollection all($columns = ['*'])
*/
class Tenant extends Model implements Contracts\Tenant
{
use Concerns\CentralConnection,
Concerns\GeneratesIds,
Concerns\HasDataColumn,
Concerns\HasInternalKeys,
Concerns\TenantRun,
Concerns\InitializationHelpers,
Concerns\InvalidatesResolverCache;
protected $table = 'tenants';
protected $primaryKey = 'id';
protected $guarded = [];
public function getTenantKeyName(): string
{
return 'id';
}
public function getTenantKey(): int|string
{
return $this->getAttribute($this->getTenantKeyName());
}
public static function current(): self|null
{
return tenant();
}
/** @throws TenancyNotInitializedException */
public static function currentOrFail(): static
{
return static::current() ?? throw new TenancyNotInitializedException;
}
public function newCollection(array $models = []): TenantCollection
{
return new TenantCollection($models);
}
protected $dispatchesEvents = [
'saving' => Events\SavingTenant::class,
'saved' => Events\TenantSaved::class,
'creating' => Events\CreatingTenant::class,
'created' => Events\TenantCreated::class,
'updating' => Events\UpdatingTenant::class,
'updated' => Events\TenantUpdated::class,
'deleting' => Events\DeletingTenant::class,
'deleted' => Events\TenantDeleted::class,
];
}