Multi-tenancy
Overview
Smart Academic Hub implements database-level multi-tenancy where each academy is a separate tenant.
Tenancy Model
Academy-Based Isolation
- Each academy is a tenant
- Data is isolated at the database query level
- Shared database with tenant filtering
Implementation
Academy Scope Middleware
The AcademyScopeMiddleware automatically filters all queries by academy ID:
// Automatically applied to all academy-scoped models
Model::where('academy_id', $currentAcademyId)->get();
Model Traits
Models use traits to automatically scope queries:
use AcademyScoped;
Data Isolation
- Foreign keys enforce relationships
- Middleware validates academy access
- Cross-tenant data access is prevented
Switching Context
Authenticated users can switch between academies if they have access to multiple tenants.
Performance
- Indexes on academy_id for optimal query performance
- Connection pooling
- Query caching strategies
Security
- Automatic filtering prevents data leaks
- Academy validation on all operations
- Audit logging for compliance
Best Practices
- Always include academy_id in relevant models
- Use middleware to enforce tenancy
- Test cross-tenant isolation
- Monitor query performance
- Implement proper error handling