good night.
I am configuring a project with spatie/multitenancy and when configuring the User model to authenticate and login, I add the UsesTenantConnection trait to the User model, the authentication is successful for the tenants, but for the owner it does not recognize the connection. If I use the UsesLandlordConnection trait or neither of the two traits in the User model, when logging in, whether in the owner or in the tenants, it recognizes the connection of the owner, and in the owner it is accessed without problems, for the owner it is fine, but for the tenants it shows a message of incorrect credentials, because it is considering the connection of the owner.
Here is the error that I get when I use the UsesTenantConnection trait and login to the owner.
Illuminate\Database\QueryException
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select * from `users` where `email` = [email protected] limit 1)
http://multitenancy_spatie.test:82/login
And here the relevant configuration files
in the kernel
protected $middlewareGroups = [
'tenant' => [
\Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
\Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class,
]
];
config('database')
[
"default" => "landlord",
"connections" => [
"tenant" => [
"driver" => "mysql",
"url" => null,
"host" => "127.0.0.1",
"port" => "3306",
"database" => null,//Tenant Autr
"username" => "root",
"password" => "",
"unix_socket" => "",
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => true,
"engine" => null,
"options" => [],
],
"landlord" => [
"driver" => "mysql",
"url" => null,
"host" => "127.0.0.1",
"port" => "3306",
"database" => "multitenancy_spatie",
"username" => "root",
"password" => "",
"unix_socket" => "",
"charset" => "utf8mb4",
"collation" => "utf8mb4_unicode_ci",
"prefix" => "",
"prefix_indexes" => true,
"strict" => true,
"engine" => null,
"options" => [],
],
],
]
config('multitenancy')
[
"tenant_finder" => "Spatie\Multitenancy\TenantFinder\DomainTenantFinder",
"tenant_artisan_search_fields" => [
"id",
],
"switch_tenant_tasks" => [
"Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask",
],
"tenant_model" => "Spatie\Multitenancy\Models\Tenant",
"queues_are_tenant_aware_by_default" => true,
"landlord_database_connection_name" => "landlord",
"current_tenant_container_key" => "currentTenant",
"actions" => [
"make_tenant_current_action" => "Spatie\Multitenancy\Actions\MakeTenantCurrentAction",
"forget_current_tenant_action" => "Spatie\Multitenancy\Actions\ForgetCurrentTenantAction",
"make_queue_tenant_aware_action" => "Spatie\Multitenancy\Actions\MakeQueueTenantAwareAction",
"migrate_tenant" => "Spatie\Multitenancy\Actions\MigrateTenantAction",
],
"queueable_to_job" => [
"Illuminate\Mail\SendQueuedMailable" => "mailable",
"Illuminate\Notifications\SendQueuedNotifications" => "notification",
"Illuminate\Events\CallQueuedListener" => "class",
"Illuminate\Broadcasting\BroadcastEvent" => "event",
],
]
.env file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:millave
APP_DEBUG=true
APP_URL=http://multitenancy_spatie.test:82
DB_CONNECTION=landlord
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=multitenancy_spatie
DB_USERNAME=root
DB_PASSWORD=
Routes file -> routes/web.php
Route::domain('{tenant}.multitenancy_spatie.test')->middleware('tenant')->group(function() {
// routes
Route::get('/', function () {
return view('auth.login');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard_tenant');
})->name('dashboard');
});
Route::domain('multitenancy_spatie.test')->group(function() {
// routes
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
});
I have reviewed on github: https://github.com/spatie/laravel-multitenancy/issues/224 , in that issue they deal with the same error, but I have not been able to find the solution to start the session.
Thanks for your help.