Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up multi tenancy in MoonShine
    moonshine laravel


How do I set up multi tenancy in MoonShine


To set up multi-tenancy in MoonShine, you can leverage the built-in support for multi-tenancy in Laravel. Here's how you can configure it:

Enabling Multi-Tenancy

1. Install the `stancl/tenancy` package:

bash
composer require stancl/tenancy

2. Publish the package's configuration files:

bash
php artisan vendor:publish --provider="Stancl\Tenancy\TenancyServiceProvider"

3. Enable multi-tenancy in the `config/tenancy.php` file by setting `'enabled' => true,`.

4. Configure your database connection for multi-tenancy in the `config/database.php` file. For example:

php
'connections' => [
    'tenant' => [
        'driver' => 'mysql',
        'host' => env('TENANT_DB_HOST', '127.0.0.1'),
        'port' => env('TENANT_DB_PORT', '3306'),
        'database' => env('TENANT_DB_DATABASE', 'forge'),
        'username' => env('TENANT_DB_USERNAME', 'forge'),
        'password' => env('TENANT_DB_PASSWORD', ''),
        'unix_socket' => env('TENANT_DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
    ],
],

Creating Tenants

1. Create a new tenant using the `Stancl\Tenancy\Database\Models\Tenant` model:

php
$tenant = Tenant::create([
    'id' => 'tenant-id',
    'domain' => 'tenant.domain.com',
]);

2. Specify the tenant-specific configuration in the `config/moonshine.php` file:

php
'theme' => function (TenantRequest $request) {
    return [
        'css' => 'path_to_theme.css',
        'colors' => [
            'primary' => 'rgb(120, 67, 233)',
            'secondary' => 'rgb(236, 65, 118)',
        ],
    ];
},

3. Customize the tenant-specific resources (views, assets, etc.) in the `resources/views/vendor/moonshine` directory.

Accessing Tenants

1. Use the `Stancl\Tenancy\Middleware\InitializeTenancyByDomain` middleware to identify the tenant based on the domain.

2. Switch between tenants using the `Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains` middleware.

By following these steps and leveraging the `stancl/tenancy` package, you can set up multi-tenancy in your MoonShine application. This allows you to create and manage separate tenants, each with its own customized admin panel and resources[4][6].

Citations:
[1] https://cloud.google.com/identity-platform/docs/multi-tenancy-quickstart
[2] https://totara.help/docs/enable-multitenancy
[3] https://www.dundas.com/support/learning/documentation/administration-configuration-customization/multi-tenancy
[4] https://moonshine.cutcode.dev/section/configuration
[5] https://frappeframework.com/docs/user/en/bench/guides/setup-multitenancy
[6] https://moonshine-laravel.com/docs/resource/appearance/appearance-index
[7] https://forum.cuba-platform.com/t/project-setup-for-a-complex-multi-tenant-application/12664
[8] https://discuss.frappe.io/t/setup-sub-domains-in-multi-tenant-sites/106888