1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 12:34:03 +00:00

separate pivot model for each approach

This commit is contained in:
Abrar Ahmad 2022-11-10 14:36:58 +05:00
parent dfe1fcecb5
commit b08961150c
4 changed files with 31 additions and 10 deletions

View file

@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Database\Models\TenantMorphPivot;
use Stancl\Tenancy\Database\Models\TenantPivot;
use Stancl\Tenancy\Events\SyncedResourceSaved;
@ -50,7 +51,7 @@ trait ResourceSyncing
public function tenants(): MorphToMany
{
return $this->morphToMany($this->getResourceTenantModelName(), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
->using(TenantPivot::class);
->using(TenantMorphPivot::class);
}
public function getResourceTenantModelName(): string // todo better name

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Models;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Stancl\Tenancy\Contracts\Syncable;
class TenantMorphPivot extends MorphPivot
{
public static function booted(): void
{
static::saved(function (self $pivot) {
$parent = $pivot->pivotParent;
if ($parent instanceof Syncable && $parent->shouldSync()) {
$parent->triggerSyncEvent();
}
});
}
}

View file

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Models;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Stancl\Tenancy\Contracts\Syncable;
class TenantPivot extends MorphPivot
class TenantPivot extends Pivot
{
public static function booted(): void
{