Skip to main content

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

  1. Always include academy_id in relevant models
  2. Use middleware to enforce tenancy
  3. Test cross-tenant isolation
  4. Monitor query performance
  5. Implement proper error handling