nexus-scholar / laravel-tenant-sqlite
Laravel package for isolated per-tenant SQLite databases
Package info
github.com/nexus-scholar/laravel-tenant-sqlite
pkg:composer/nexus-scholar/laravel-tenant-sqlite
Requires
- php: ^8.3
- ext-pdo: *
- ext-pdo_sqlite: *
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^v11.1.0
- pestphp/pest: ^v4.6.3
- pestphp/pest-plugin-laravel: ^v4.1.0
README
A robust and secure Laravel package for implementing multi-tenancy using an isolated SQLite database file for each tenant.
Instead of adding a tenant_id column to every table, this package gives each user (or organization) their own dedicated SQLite file. This approach guarantees complete data isolation, trivializes single-tenant backups, and simplifies data portability.
Features
- 🔒 Absolute Isolation: Tenants physically cannot query each other's data.
- 📦 Simple Backups: Back up a tenant's entire dataset by copying a single
.sqlitefile. - 🚀 Dynamic Connections: Seamlessly connects Eloquent models to the correct file at runtime.
- 🛠️ Full Lifecycle Management: Commands to create, migrate, inspect, backup, archive, and purge tenant databases.
- 🌐 HTTP & Queue Support: Includes middleware to automatically resolve tenants for web requests and background jobs.
- ⚡ Pest Tested: Thoroughly tested for complete reliability and data isolation.
Documentation
For a comprehensive guide on getting started, setting up models, and writing migrations, please read our beginner's guide:
For architectural details, consult the docs directory.
Quick Start
Installation
composer require nexus-scholar/laravel-tenant-sqlite php artisan tenant-database:install php artisan migrate
Basic Usage
- Create a tenant migration in
database/migrations/tenant/0001_create_projects_table.php. - Use the
UsesTenantConnectiontrait on your models:
use Illuminate\Database\Eloquent\Model; use NexusScholar\LaravelTenantSqlite\Concerns\UsesTenantConnection; class Project extends Model { use UsesTenantConnection; }
- Provision a tenant and run their migrations:
use NexusScholar\LaravelTenantSqlite\Facades\TenantDatabase; $user = User::find(1); TenantDatabase::provision($user); TenantDatabase::migrate($user);
- Use the middleware to automatically route requests to the correct database:
// routes/web.php Route::middleware(['auth', 'tenant'])->group(function () { Route::get('/projects', function () { // This automatically queries the authenticated user's SQLite file! return App\Models\Project::all(); }); });
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.