mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:14:03 +00:00
Shared users - complete
This commit is contained in:
parent
daae67c0f7
commit
b7c8f1fba7
12 changed files with 488 additions and 56 deletions
|
|
@ -3,14 +3,32 @@
|
|||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
|
||||
trait ResourceSyncing
|
||||
{
|
||||
public static function bootResourceSyncing()
|
||||
{
|
||||
static::saving(function (Syncable $model) {
|
||||
event(new SyncedResourceSaved($model, tenant()));
|
||||
static::saved(function (Syncable $model) {
|
||||
/** @var ResourceSyncing $model */
|
||||
|
||||
$model->triggerSyncEvent();
|
||||
});
|
||||
|
||||
static::creating(function (self $model) {
|
||||
if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||
$model->setAttribute(
|
||||
$model->getGlobalIdentifierKeyName(),
|
||||
app(UniqueIdentifierGenerator::class)->generate($model)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function triggerSyncEvent()
|
||||
{
|
||||
/** @var Syncable $this */
|
||||
event(new SyncedResourceSaved($this, tenant()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
src/Database/Models/TenantPivot.php
Normal file
15
src/Database/Models/TenantPivot.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class TenantPivot extends Pivot
|
||||
{
|
||||
public static function booted()
|
||||
{
|
||||
static::saved(function (self $pivot) {
|
||||
$pivot->pivotParent->triggerSyncEvent();
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue