我在 Laravel 应用程序中实现 Spatie Laravel 多租户时遇到问题。尝试运行该应用程序时,我收到以下错误:
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist
SELECT * FROM `users`
这是我的设置:
数据库配置: 我为租户和房东有单独的数据库。租户数据库连接配置如下:
'tenant' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => null,
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'landlord' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
多租户配置: 在我的 config/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;
use Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask;
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\PrefixCacheTask::class,
SwitchTenantDatabaseTask::class,
// \Spatie\Multitenancy\Tasks\SwitchRouteCacheTask::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' =>null,
/*
* This key will be used to bind the current tenant in the container.
*/
'current_tenant_container_key' => 'currentTenant',
/**
* Set it to `true` if you like to cache the tenant(s) routes
* in a shared file using the `SwitchRouteCacheTask`.
*/
'shared_routes_cache' => false,
/*
* 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',
],
/*
* Jobs tenant aware even if these don't implement the TenantAware interface.
*/
'tenant_aware_jobs' => [
// ...
],
/*
* Jobs not tenant aware even if these don't implement the NotTenantAware interface.
*/
'not_tenant_aware_jobs' => [
// ...
],
];
数据库环境变量: 在我的.env文件中,我已经为楼主配置了数据库连接:
DB_CONNECTION=landlord
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
错误上下文: 当应用程序尝试访问用户表时,会发生该错误。但是,在多租户架构中,每个租户都应该有自己的用户表。看起来应用程序正在尝试访问房东数据库中的用户表而不是租户数据库。
问题:
如何解决此问题并确保应用程序在使用 Spatie Laravel Multitenancy 时根据当前租户访问正确的用户表?
我按照文档在 Laravel 应用程序中实现了 Spatie Laravel 多租户,并为租户和房东配置了单独的数据库。我希望应用程序能够根据当前租户正确访问数据库表。具体来说,在访问用户表时,我希望应用程序使用与当前租户数据库关联的用户表。
经过几个小时的测试,答案是将租户连接设置为默认连接
DB_CONNECTION=tenant