Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with querying code at console and seeders? #60

Open
n3storm opened this issue Oct 18, 2023 · 0 comments
Open

How to deal with querying code at console and seeders? #60

n3storm opened this issue Oct 18, 2023 · 0 comments

Comments

@n3storm
Copy link

n3storm commented Oct 18, 2023

I've found that querying models on seeders are using using default root tenant so behaviour is unexpected.

$tenants = app('rinvex.tenants.tenant')->all();
foreach ($tenants as $tenant) {
     $all_users = User::withAllTenants($tenant)->get(); <-- Not working, gets main domain users
     $all_users = User::withAnyTenants($tenant)->get(); <-- Not working, gets main domain users
     $all_users = $tenant->entries(User::class)->get(); <-- Not working, gets main domain users
}

I finally made my own trait that works thanks to "withoutGlobalScopes":


trait ForTenant
{
    public function scopeForTenant($builder, $tenant)
    {
        return (new static())->newQuery()->withoutGlobalScopes(['tenantable'])->whereHas('tenants', function ($builder) use ($tenant) {
            $builder->where($key = $tenant->getKeyName(), $tenant->{$key})->where('is_active', true);
        });
    }
}

Could there be a better solution for this?

A)
withAnyTenantsWithoutGlobalScopes methods?

B)
initialize withAnyTenant methods with withoutGlobalScopes when application running outside request cycle?

C)
I still find strange I am the first to deal with this issue and there must be something wrong with my setup...

3 tenants with 3 subdomains created with a seeder:

  • domain.org
  • b.domain.org
  • c.domain.org

Config/app.php entry:

    'domains' => [
        env('MULTITENANT_DOMAIN', 'another.org') => [],
    ],

and at .env file:

MULTITENANT_DOMAIN=domain.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant