tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

Allow domains() relationship to support column

Open nathanwuiske opened this issue 3 years ago • 1 comments

If using Domains/Subdomains, we are forced to use a Domain Model for our Tenant Model:

public function domains()
    {
        return $this->hasMany(config('tenancy.domain_model'), 'tenant_id');
    }

But what about in scenarios where Tenants have only one domain (a single subdomain), which could therefore be stored on the Tenant Model as a column, rather than an intermediate Domain model?

nathanwuiske avatar Jul 17 '22 23:07 nathanwuiske

It is true most of the users have only one domain per tenant. Storing the domain in a column would save a query. Adding such a feature would probably mean re-writing a lot of the package.

In our solution, we also allow only one domain per tenant. What we've done is we added a global helper that retrieves the domain for us. Since the tenant is identified, you can use that in your advantage :

if (! function_exists('tenant_domain')) {
    function tenant_domain()
    {
        return tenant() ? tenant()->domains()->sole('domain')->domain : null;
    }
}

stein-j avatar Jul 22 '22 19:07 stein-j

@stein-j I am been using your solution for my use case. But can we handle the CRUD of the domain column using some feature flag in config to handle a single domain? I am not aware how much complexity is involved, but yes it definitely means a whole lot of changes.

chinmaypurav avatar Mar 02 '23 09:03 chinmaypurav

Being implemented in v4 now.

stancl avatar Oct 30 '23 12:10 stancl