pg-tenancy/laravel-pg-tenancy
Composer 安装命令:
composer require pg-tenancy/laravel-pg-tenancy
包简介
Laravel multi-tenancy for PostgreSQL schema isolation with connection pooling (subdomain, path, team modes)
README 文档
README
Multi-tenancy for Laravel with PostgreSQL schema isolation and connection pooling. Supports subdomain, path, or Team model based tenancy modes.
Installation
- Require the package (adjust VCS path if local):
composer require pg-tennancy/laravel-pg-tenancy
- Publish config and migrations:
php artisan vendor:publish --provider="PgTenancy\\PgTenancyServiceProvider" --tag=tenancy-config php artisan vendor:publish --provider="PgTenancy\\PgTenancyServiceProvider" --tag=tenancy-migrations php artisan vendor:publish --provider="PgTenancy\\PgTenancyServiceProvider" --tag=tenancy-tenant-migrations
-
Configure
config/tenancy.phpand ensure a privilegedsystem_connectioncan create schemas/roles. -
Add middleware to HTTP kernel or route group:
// app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ // ... \PgTenancy\Http\Middleware\IdentifyTenant::class, ], ];
Modes
- Subdomain:
tenant1.example.com(configurebase_domain) - Path:
example.com/tenant1(configurepath_segment_index) - Team: authenticated user's team (configure relation name)
Schema Isolation
Each tenant gets its own PostgreSQL schema and role. The package sets the Postgres search_path to {schema},public on a per-request tenant connection.
Commands
php artisan tenancy:tenant:create {slug} {--domain=} {--schema=} {--team-id=}php artisan tenancy:tenant:delete {id-or-slug}php artisan tenancy:migrate {--fresh} {--seed}
Tests
composer test
License
MIT
Programmatic Tenant Creation (Sign-up)
Preferred flow: create a User, then create a Team for that user. The package provisions the tenant schema via the Team observer and automatically runs tenant migrations.
use PgTenancy\Models\Team; // After creating $user $team = Team::createForUser('Acme Inc', $user); // A tenant record is created and schema is provisioned automatically (observer)
Add tenancy to your registration flow (Livewire Volt example)
- Ensure team-based tenancy mode:
// config/tenancy.php return [ 'mode' => 'team', ];
- Register route middleware so tenancy resolves after auth:
// bootstrap/app.php ->withMiddleware(function (\Illuminate\Foundation\Configuration\Middleware $middleware) { $middleware->alias([ 'tenant' => \PgTenancy\Http\Middleware\IdentifyTenant::class, ]); }) // routes/web.php Route::view('dashboard', 'dashboard')->middleware(['auth', 'tenant', 'verified']);
- Extend your register component to accept a team name and create the team after user creation:
// resources/views/livewire/auth/register.blade.php (excerpt) use PgTenancy\\Models\\Team; public string $team_name = ''; public function register(): void { // ... validate + create $user + Auth::login($user) $teamName = trim($this->team_name) !== '' ? $this->team_name : ($user->name . "'s Team"); Team::createForUser($teamName, $user); // redirect to dashboard } // Add an input to the form <flux:input wire:model="team_name" :label="__('Team name')" type="text" autocomplete="organization" />
- Show current tenant schema (optional):
// resources/views/dashboard.blade.php (excerpt) $schemaName = DB::selectOne('select current_schema() as schema')->schema ?? null;
Notes:
- On
TenantCreated, the package runs tenant migrations indatabase/migrations/tenanton the tenant connection. - Team slugs and tenant schemas are made unique automatically (even for duplicate team names).
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-11