fantismic/tenancy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fantismic/tenancy

Composer 安装命令:

composer require fantismic/tenancy

包简介

A package to manage multi-tenancy in Laravel applications.

README 文档

README

Fantismic/Tenancy

A Laravel package to build multi-tenant applications with separate databases per tenant on a single domain.

Features

  • Tenant Provisioning: Create tenant-specific databases and database users automatically.
  • Multi-Database Support: Each tenant gets its own MySQL database connection.
  • Migrations & Seeders: Run migrations and seeders per tenant.
  • Tenant Isolation: Use the UsesTenantConnection trait to isolate Eloquent models to the tenant database.
  • Middleware: Automatically initialize tenant context based on authenticated user.
  • Artisan Commands: Manage tenant migrations via console command.
  • Admin Panel: Out-of-the-box routes, controllers, and views for tenant administration (list, migrate, sync users, delete).

Requirements

  • PHP >= 8.1
  • Laravel >= 10.0
  • MySQL

Installation

composer require fantismic/tenancy

Service Provider & Facades

This package uses auto-discovery. The following service providers and facades are registered:

  • Service Providers:

    • Fantismic\Tenancy\TenancyServiceProvider
  • Facades:

    • Tenancy (alias for Fantismic\Tenancy\Helpers\TenantManager)
    • TenantAdmin (alias for Fantismic\Tenancy\Services\TenantAdminService)

Database Migrations

Publish and run the central migrations:

php artisan vendor:publish --provider="Fantismic\Tenancy\TenancyServiceProvider" --tag="migrations"
php artisan migrate

This will create:

  • tenants table
  • tenant_user pivot table
  • Tenant-specific migration stub directory at database/migrations/tenant

Configuration

No configuration file is published. Tenant connection settings are stored in the connection JSON column of the tenants table. You can customize connection parameters when creating a tenant.

Usage

Creating a Tenant

Use the Tenancy facade to create a new tenant:

use Fantismic\Tenancy\Facades\Tenancy;

$tenant = Tenancy::createTenant(
    'Tenant Name',            // Tenant display name
    [                         // Connection details for admin user
        'driver'         => 'mysql',
        'host'           => '127.0.0.1',
        'database'       => 'admin_db',
        'username'       => 'root',
        'password'       => 'secret',
    ],
    [                         // Options
        'create_db' => true,  // Create database & user (default: true)
        'migrate'   => true,  // Run tenant migrations (default: true)
        'seed'      => null,  // Seeder class to run (optional)
    ]
);

This will:

  1. Generate a UUID for the tenant.
  2. Create a database named tenant_{uuid} and a user u_{uuid} with random password.
  3. Store the connection JSON in the tenants table.
  4. Run tenant migrations in database/migrations/tenant.
  5. Return the Tenant model instance.

Artisan Command

Run migrations for an existing tenant:

php artisan tenants:migrate {tenant_id}

Eloquent Models

Use the UsesTenantConnection trait on any model that should use the tenant database:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Fantismic\Tenancy\Traits\UsesTenantConnection;

class Invoice extends Model
{
    use UsesTenantConnection;
    // ...
}

Middleware

Protect tenant routes and initialize tenant context:

use Fantismic\Tenancy\Http\Middleware\InitializeTenant;

Route::middleware([
    'auth',
    InitializeTenant::class,
])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
});

Admin Panel

The package provides a simple administrative UI. Publish views:

php artisan vendor:publish --provider="Fantismic\Tenancy\TenancyServiceProvider" --tag="views"

Then visit /admin/tenants to manage tenants.

Supported actions:

  • List tenants
  • Migrate tenant database
  • Sync users from central to tenant pivot
  • Delete tenant and drop its database

Facades & Services

  • Tenancy (TenantManager): Tenant creation, migrations, and seeding.
  • TenantAdmin (TenantAdminService): Fetch, migrate, sync users, and delete tenants.

Events & Listeners

The package listens to the Login event to set the current tenant in session:

  • Fantismic\Tenancy\Listeners\SetTenantSession

Contributing

Feel free to submit issues or pull requests. Follow PSR-12 coding standards and include tests for new features.

License

MIT License. See LICENSE for details.

fantismic/tenancy 适用场景与选型建议

fantismic/tenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 117 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 fantismic/tenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fantismic/tenancy 我们能提供哪些服务?
定制开发 / 二次开发

基于 fantismic/tenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 117
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-14