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).
pg-tenancy/laravel-pg-tenancy 适用场景与选型建议
pg-tenancy/laravel-pg-tenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「schema」 「postgresql」 「laravel」 「multi-tenancy」 「isolation」 「connection pooling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pg-tenancy/laravel-pg-tenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pg-tenancy/laravel-pg-tenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pg-tenancy/laravel-pg-tenancy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Extension for Opis JSON Schema
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Sql dumps containing databases for the linna apps.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-11