How tarikulcodes/laravel-multi-tenant works
Project type and purpose
• This is a demo multi-tenant SaaS application built with Laravel 10. It demonstrates how a single codebase can serve multiple tenant domains, isolating each tenant’s data while sharing the core infrastructure.
File structure
• Top-level folders follow the standard Laravel MVC layout:
• app/Http/Controllers (separated into App-scoped and global auth controllers)
• app/Models (User, Tenant)
• config/tenancy.php and a TenancyServiceProvider in app/Providers
• database/migrations (central and tenant directories)
• resources/views (Blade templates separated by scope: app, auth, tenants)
• routes (web.php, tenant.php, tenant-auth.php, api.php)
• Notice “database/migrations/tenant” vs “database/migrations” – indicates two logical databases.
• Vite, Tailwind, and Blade live in resources/js, resources/css, and resources/views.
• Jobs (SeedTenantJob) and queue config hint at background workers.
README
• Confirms Laravel 10, Blade, Vite JS, Tailwind CSS, Tenancy for Laravel, and Spatie Permission.
• Explains multi-tenant concept: one instance, multiple customers, isolated data.
Main components
a. Web Browser / Client
• Static assets (CSS/JS via Vite)
• HTTP(S) requests to tenant domains (e.g. tenant1.localhost) or main domain
b. Load Balancer or Reverse Proxy (optional for production)
c. Laravel Application Container
• Public/index.php → bootstrap/app.php → HTTP Kernel
• Middleware stack including tenancy identification (identify tenant from subdomain or path)
• Service Providers: TenancyServiceProvider, AuthServiceProvider, RouteServiceProvider
• Routing: web.php (central) vs tenant.php (tenant routes)
• Controllers: App-scoped vs global, Profile, TenantController
• Blade View Renderer & Vite asset pipeline
• Queue Worker (processes jobs like SeedTenantJob)
d. Databases
• Central Database: tenants table, domains table, user_global tables, permissions tables
• Tenant-specific Database(s): user, password_resets, personal_access_tokens, permission tables (scoped per tenant)
• Eloquent ORM manages connections based on tenancy context
e. Storage & Cache
• Filesystem (local/public) for uploads
• Cache & sessions (configured in config/cache.php and config/session.php)
• Queue backend (database or Redis)
f. External Services
• Mail (configured in config/mail.php)
• Broadcasting (Pusher, Redis, etc.)
• Third-party APIs or payment gateways (via config/services.php)
Relationships and data flow
• Client → (Load Balancer) → Web Server (Nginx/Apache) → index.php
• index.php loads the Laravel kernel → Middleware identifies tenant → Service Providers boot
• Router dispatches to either central or tenant route group → Controller logic
• Controllers interact with models → Eloquent sends queries to central or tenant DB
• Views rendered with Blade and assets injected via Vite → Response to client
• Jobs (e.g., Tenant Seeding) dispatched to queue → Queue Worker picks up job → writes to tenant DB
• File uploads go to storage/app/public, served via symbolic link to public/storage
Architectural patterns and design principles
• MVC (Model-View-Controller) for request handling
• Service Provider pattern for modular bootstrapping (especially tenancy)
• Middleware chain for cross-cutting concerns (auth, tenancy, CSRF)
• Repository/ORM (Eloquent) for data access
• Tenancy pattern: central vs tenant context, subdomain routing
• Command Bus/Queue for asynchronous processing
Technologies and frameworks to annotate
• Laravel 10 core
• Tenancy for Laravel (tenant resolution, multi-DB management)
• Spatie Laravel-Permission (role and permission management in both contexts)
• Blade templating (views/components)
• Vite JS + Tailwind CSS (asset bundling and styling)
• PHP-FPM, Nginx/Apache (deployment), MySQL/PostgreSQL