Matt Presland
Matt Presland

Reputation: 301

Laravel Hyn\Multi-Tenant Database [tenant] not configured

I am working on a multi-tenant laravel application, and have run into an issue with the hyn\multi-tenant package. The documentation for hyn\multi-tenant states that the tenant database connection will be handled by the package, and that as long as the system connection is available and the user has privileges to add and modify databases, the package will handle all tenant database connections.

Upon trying to create a tenant in my application I get the error: Database [tenant] is not configured.

I have seen many answers to this issue on SO, however they all refer to the Customer model, or localhost configurations. Hyn removed the Customer model, and the issue I am having is also happening on my DigitalOcean server published through Laravel Forge.

I would be grateful to anyone that might be able to provide some assistance.

My .env (local)

APP_NAME="Multi-Tenant"
APP_ENV=local
APP_KEY=base64:j1aLzU7m5LWK1keo/FjgbtpwTpVZ1NBj29zuXIByHek=
APP_DEBUG=true
APP_URL_BASE=localhost:8888/lms/public
APP_URL=http://${APP_URL_BASE}

LOG_CHANNEL=stack

DB_CONNECTION=system
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=lmssystem
DB_USERNAME=lmssystem
DB_PASSWORD=lmssystem

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

LIMIT_UUID_LENGTH32=true

My database.php

return [
'default' => env('DB_CONNECTION', 'system'),
'connections' => [

        'system' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '8889'),
            'database' => env('DB_DATABASE', 'lmssystem'),
            'username' => env('DB_USERNAME', 'lmssystem'),
            'password' => env('DB_PASSWORD', 'lmssystem'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => 'InnoDB',
        ],
    ],
 'migrations' => 'migrations',
'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

Upvotes: 3

Views: 8994

Answers (4)

Hassan Elshazly Eida
Hassan Elshazly Eida

Reputation: 849

if it related to Livewire problem,

Open the config/livewire.php file and change this:

'middleware_group' => ['web'],

to this:

'middleware_group' => [
    'web',
    'universal',
    InitializeTenancyByDomain::class, // or whatever tenancy middleware you use
],

Upvotes: 1

Roberth
Roberth

Reputation: 1

create a "tenant" connection on the config/database.php file

'tenant' => [
        'driver' => 'mysql',
        'host' => env('TENANCY_HOST', '127.0.0.1'),
        'port' => env('TENANCY_PORT', '3306'),
        'database' => env('TENANCY_DATABASE', 'tenancy'),
        'username' => env('TENANCY_USERNAME', 'tenancy'),
        'password' => env('TENANCY_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ];

Upvotes: 0

Soheil Ghasemian
Soheil Ghasemian

Reputation: 37

this code may be solve your problem

config/database.php :

'connections' => [

    'system' => [
        'driver' => env('DB_DRIVER', 'mysql'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', '123'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'tenant' => [
        'driver' => env('DB_DRIVER', 'mysql'),
        'host' => '',
        'port' => env('DB_PORT', '3306'),
        'database' => '',
        'username' => '',
        'password' => '',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

],

Upvotes: 0

Matt Presland
Matt Presland

Reputation: 301

This issue turned out to be a logic error in my application not a database configuration issue. I was forced to deviate from Ashok's article on Medium [https://medium.com/@ashokgelal/a-full-featured-multi-tenant-app-with-laravel-part-1-4049a3cc229d][1] since the Hyn\Multi-Tenant package no longer supports the customer model.

After developing my own client model, and extending the existing Website and Hostname models to interact with the client model, I had to re-write the tenant:create command. In this, I created websites and hostnames directly from their extended models, rather than via the repositories as per the Hyn documentation (copied below)

Tenancy is heavily driven by events. For event listeners to properly work, you have to use the repositories to create new websites and hostnames. use Hyn\Tenancy\Models\Website; use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository;

$website = new Website;
app(WebsiteRepository::class)->create($website);
dd($website->uuid); 

$hostname = new Hostname;
$hostname->fqdn = 'luceos.demo.app';
app(HostnameRepository::class)->attach($hostname, $website);

Creating via the repositories resolved the:

Database [tenant] not configured

Error.

Upvotes: 1

Related Questions