1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 18:04:03 +00:00

Custom tenant collections

This commit is contained in:
Samuel Štancl 2020-05-14 02:29:14 +02:00
parent 1a8d150f2c
commit 5e99fca070
4 changed files with 86 additions and 1 deletions

View file

@ -7,7 +7,18 @@ namespace Stancl\Tenancy\Contracts;
*/
interface Tenant
{
/** Get the name of the key used for identifying the tenant. */
public function getTenantKeyName(): string;
/** Get the value of the key used for identifying the tenant. */
public function getTenantKey(): string;
/** Get the value of an internal key. */
public function getInternal(string $key);
/** Set the value of an internal key. */
public function setInternal(string $key, $value);
/** Run a callback in this tenant's environment. */
public function run(callable $callback);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Stancl\Tenancy\Database\Collections;
use Illuminate\Database\Eloquent\Collection;
use Stancl\Tenancy\Contracts\Tenant;
/**
* @property Tenant[] $items
* @method void __construct(Tenant[] $items = [])
* @method Tenant[] toArray()
* @method Tenant offsetGet($key)
* @method Tenant first()
*/
class TenantCollection extends Collection
{
public function runForEach(callable $callable): self
{
tenancy()->runForMultiple($this->items, $callable);
return $this;
}
}

View file

@ -6,6 +6,7 @@ use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Database\Collections\TenantCollection;
use Stancl\Tenancy\Database\Concerns;
/**
@ -13,6 +14,8 @@ use Stancl\Tenancy\Database\Concerns;
* @property Carbon $created_at
* @property Carbon $updated_at
* @property array $data
*
* @method TenantCollection all()
*/
class Tenant extends Model implements Contracts\Tenant
{
@ -36,6 +39,11 @@ class Tenant extends Model implements Contracts\Tenant
return $this->getAttribute($this->getTenantKeyName());
}
public function newCollection(array $models = []): TenantCollection
{
return new TenantCollection($models);
}
public function getCasts()
{
return array_merge($this->dataColumnCasts(), [