tenancy
tenancy copied to clipboard
Allow domains() relationship to support column
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?
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 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.
Being implemented in v4 now.