Tenant Portfolios
Overview
Tenant Portfolios provide a higher-level organization structure above Tenant Clusters, allowing for the grouping of multiple clusters into logical collections. This hierarchical approach enables more effective management of large multi-tenant environments. Each portfolio can have a designated "Managing Tenant Cluster" which is responsible for overseeing the entire portfolio.
Implementation
Tenant Portfolios are implemented as a one-to-many relationship with tenant clusters, with an additional reference for the managing cluster:
- A Portfolio can have many Tenant Clusters
- A Tenant Cluster belongs to one Portfolio (or none)
- A Portfolio can have one Managing Tenant Cluster
The system uses a tenant_portfolio_id foreign key in the tenant_clusters table and a managing_tenant_cluster_id foreign key in the tenant_portfolios table to establish these relationships.
Database Schema
The implementation involves two primary database tables:
-
tenant_portfoliostable:id: Primary keyname: Name of the portfolioslug: URL-friendly version of the name (auto-generated)description: Optional description of the portfoliomanaging_tenant_cluster_id: Foreign key to thetenant_clusterstable (nullable), indicating the managing cluster for this portfolio- Standard timestamp fields
-
tenant_clusterstable includes:tenant_portfolio_id: Foreign key to the tenant_portfolios table (nullable)
Models
TenantPortfolio Model
The TenantPortfolio model provides a way to create and manage portfolios.
The TenantPortfolio model uses an observer (TenantPortfolioObserver) which ensures that a portfolio with existing tenant clusters cannot be updated to have no managing_tenant_cluster_id. This enforces that a Portfolio with clusters must have a Managing Tenant Cluster.
TenantCluster Model
The TenantCluster model includes a relationship to portfolios:
public function portfolio()
{
return $this->belongsTo(TenantPortfolio::class, 'tenant_portfolio_id');
}
public function isPortfolioManager(): bool
{
return $this->portfolio && $this->portfolio->managing_tenant_cluster_id == $this->id;
}
Tenant Model
The Tenant model includes a method to access its portfolio through the cluster:
public function portfolio(): ?TenantPortfolio
{
return $this->cluster ? $this->cluster->portfolio : null;
}
public function isPortfolioManager(): bool
{
return $this->isClusterManager() && $this->cluster->isPortfolioManager();
}
User Interface
Tenant Portfolios can be managed in the Landlord admin area, within the "Multitenancy" section. The interface provides:
- Creating Portfolios: Administrators can create portfolios with a name and description
- Managing Portfolios: Administrators can edit or delete existing portfolios
- Assigning Tenant Clusters: Tenant Clusters can be assigned to portfolios through the Portfolio management interface
- Setting the Managing Tenant Cluster: The Managing Tenant Cluster can be assigned or changed from the Portfolio management interface
Usage
Creating a Tenant Portfolio
- Navigate to the Tenant Portfolios page in the Multitenancy section
- Click "New Tenant Portfolio"
- Provide a name and optional description
- Save the portfolio
Assigning Tenant Clusters to a Portfolio
- From the Tenant Portfolios page, click on a portfolio
- In the Tenant Clusters tab, click "Attach Existing"
- Select one or more clusters to add to the portfolio
- Alternatively, edit a tenant cluster directly and select a portfolio from the dropdown
Setting/Changing the Managing Tenant Cluster
- Navigate to the Tenant Portfolios page
- Click on the portfolio you wish to manage
- Select a tenant cluster to serve as the managing cluster for the portfolio
- Note: A portfolio that has tenant clusters must always have a managing tenant cluster
Accessing Data Across a Portfolio
The ScopedToTenantTrait provides a convenient way to filter data by portfolio:
// Get all records in the current tenant's portfolio
$records = YourModel::ofCurrentTenantPortfolio()->get();
Best Practices
- Hierarchical Organization: Use portfolios to create logical groupings of related tenant clusters
- Managing Cluster Selection: Choose a managing tenant cluster that has appropriate oversight responsibilities for the entire portfolio
- Portfolio Organization: Consider organizing portfolios by:
- Business divisions or departments
- Geographic regions or territories
- Client categories or market segments
- Regular Maintenance: Periodically review portfolio and cluster assignments to ensure they align with organizational needs