1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 12:34:03 +00:00
This commit is contained in:
Abrar Ahmad 2022-11-08 14:49:01 +05:00
parent b7a6953231
commit 8c81ef2a8d
6 changed files with 428 additions and 7 deletions

View file

@ -14,7 +14,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
*/
interface SyncMaster extends Syncable
{
public function tenants(): BelongsToMany;
//public function tenants(): BelongsToMany;
public function getTenantModelName(): string;
}

View file

@ -4,10 +4,11 @@ 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 Pivot
class TenantPivot extends MorphPivot
{
public static function booted(): void
{

View file

@ -46,10 +46,10 @@ class UpdateSyncedResource extends QueueableListener
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
// relationship already loaded and cached. For this reason, we refresh the relationship.
$centralModel->load('tenants');
$centralModel->load('resources');
/** @var TenantCollection $tenants */
$tenants = $centralModel->tenants;
$tenants = $centralModel->resources;
return $tenants;
}
@ -80,7 +80,7 @@ class UpdateSyncedResource extends QueueableListener
return ((string) $model->pivot->tenant_id) === ((string) $tenant->getTenantKey());
};
$mappingExists = $centralModel->tenants->contains($currentTenantMapping);
$mappingExists = $centralModel->resources->contains($currentTenantMapping);
if (! $mappingExists) {
// Here we should call TenantPivot, but we call general Pivot, so that this works
@ -89,12 +89,12 @@ class UpdateSyncedResource extends QueueableListener
/** @var Tenant */
$tenant = $event->tenant;
$centralModel->tenants()->attach($tenant->getTenantKey());
$centralModel->resources()->attach($tenant->getTenantKey());
});
}
/** @var TenantCollection $tenants */
$tenants = $centralModel->tenants->filter(function ($model) use ($currentTenantMapping) {
$tenants = $centralModel->resources->filter(function ($model) use ($currentTenantMapping) {
// Remove the mapping for the current tenant.
return ! $currentTenantMapping($model);
});