laravel-multitenancy: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected.

It seems that I can’t reach the top level domain with landlord database connection.

However, tenant connection is working fine. But when I’m a top level domain it gives me

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select * from `users` where `email` = isaacdarcilla@gmail.com and `users`.`deleted_at` is null limit 1)

database.php

    'default' => env('DB_CONNECTION', 'tenant'),

    'connections' => [

        'tenant' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => null,
            ....
        ],

        'landlord' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' =>  'master',
            ....
        ],

    ],

.env


APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:uSoRKgTycrSZ3tUV+jUg5SploFri6/KqnzbBSdZfjhQ=
APP_DEBUG=true
APP_URL=http://schoolaide.test

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=tenant
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=master
DB_USERNAME=root
DB_PASSWORD=
....

web.php

Route::group(['middleware' => 'guest'], function () { 
    Route::get('login')->name('login')->uses('Auth\LoginController@showLoginForm');
    Route::post('login')->name('login.attempt')->uses('Auth\LoginController@login');
    Route::get('register')->name('register')->uses('Auth\RegisterController@showRegisterForm');
    Route::get('forgot/password')->name('forgot.password')->uses('Auth\ResetPasswordController@showForgotForm');
    Route::post('register')->name('register.attempt')->uses('Auth\RegisterController@register');
    Route::post('forgot/password/sendEmail')->name('forgot.password.email')->uses('Auth\ResetPasswordController@sendEmail');
    Route::get('register/onboarding/{id}')->name('register.onboarding')->uses('Auth\RegisterController@showOnboarding');
    Route::post('register/verification')->name('register.verification')->uses('Auth\RegisterController@verify');
    Route::put('register/verification/resend/{id}')->name('register.resend')->uses('Auth\RegisterController@resend');
});

Route::middleware('tenant')->group(function() {

});

Route::group(['middleware' => 'auth'], function () { 
    Route::get('/')->name('dashboard')->uses('DashboardController');
});

Route::get('logout')->name('logout')->uses('Auth\LoginController@logout');

multitenancy.php

<?php

use Illuminate\Broadcasting\BroadcastEvent;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Notifications\SendQueuedNotifications;
use Spatie\Multitenancy\Actions\ForgetCurrentTenantAction;
use Spatie\Multitenancy\Actions\MakeQueueTenantAwareAction;
use Spatie\Multitenancy\Actions\MakeTenantCurrentAction;
use Spatie\Multitenancy\Actions\MigrateTenantAction;
use Spatie\Multitenancy\Models\Tenant;

return [
    /*
     * This class is responsible for determining which tenant should be current
     * for the given request.
     *
     * This class should extend `Spatie\Multitenancy\TenantFinder\TenantFinder`
     *
     */
    'tenant_finder' => Spatie\Multitenancy\TenantFinder\DomainTenantFinder::class,

    /*
     * These fields are used by tenant:artisan command to match one or more tenant
     */
    'tenant_artisan_search_fields' => [
        'id',
    ],

    /*
     * These tasks will be performed when switching tenants.
     *
     * A valid task is any class that implements Spatie\Multitenancy\Tasks\SwitchTenantTask
     */
    'switch_tenant_tasks' => [
        Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask::class,
    ],

    /*
     * This class is the model used for storing configuration on tenants.
     *
     * It must be or extend `Spatie\Multitenancy\Models\Tenant::class`
     */
    'tenant_model' => Tenant::class,

    /*
     * If there is a current tenant when dispatching a job, the id of the current tenant
     * will be automatically set on the job. When the job is executed, the set
     * tenant on the job will be made current.
     */
    'queues_are_tenant_aware_by_default' => true,

    /*
     * The connection name to reach the tenant database.
     *
     * Set to `null` to use the default connection.
     */
    'tenant_database_connection_name' => 'tenant',

    /*
     * The connection name to reach the landlord database
     */
    'landlord_database_connection_name' => 'landlord',

    /*
     * This key will be used to bind the current tenant in the container.
     */
    'current_tenant_container_key' => 'currentTenant',

    /*
     * You can customize some of the behavior of this package by using our own custom action.
     * Your custom action should always extend the default one.
     */
    'actions' => [
        'make_tenant_current_action' => MakeTenantCurrentAction::class,
        'forget_current_tenant_action' => ForgetCurrentTenantAction::class,
        'make_queue_tenant_aware_action' => MakeQueueTenantAwareAction::class,
        'migrate_tenant' => MigrateTenantAction::class,
    ],

    /*
     * You can customize the way in which the package resolves the queuable to a job.
     *
     * For example, using the package laravel-actions (by Loris Leiva), you can
     * resolve JobDecorator to getAction() like so: JobDecorator::class => 'getAction'
     */
    'queueable_to_job' => [
        SendQueuedMailable::class => 'mailable',
        SendQueuedNotifications::class => 'notification',
        CallQueuedListener::class => 'class',
        BroadcastEvent::class => 'event',
    ],
];

Top level domain

image

Sub domain which I was able to login

image

ANY WORKAROUND?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (3 by maintainers)

Most upvoted comments

@isaacdarcilla, how can you access the landlord if all your routes require the middleware tenant? You have no root route for the landlord.

  1. Switch your default connection to DB_CONNECTION=landlord
  2. Create routes accessible to landlord or tenants
  3. Add a tenant to the landlord database
  4. you will rock!

See it, I’m using your code: image

routes/web.php:

Route::get('/', function () {
    if (\Spatie\Multitenancy\Models\Tenant::checkCurrent()) {
        return "I'm a tenant";
    }

    return "I'm the landlord";
});

I’m closing the issue because isn’t related to a package bug, but feel free to continue the discussion. Thanks.

Make sure you have configured the middlewares