Heim > Fragen und Antworten > Hauptteil
Wie ändere ich die Datenbank in Laravels auth.php (config/auth.php)? Ich arbeite mit mehreren Datenbanken und möchte Benutzer in einer anderen Datenbank speichern.
P粉0266659192023-09-09 15:52:38
首先,您应该在config/database.php中定义连接:
'connections' => [ 'global' => [ 'driver' => 'mysql', 'host' => env('first_db_name', '127.0.0.1'), ... ], 'tennant' => [ 'driver' => 'sqlite', 'host' => env('sec_db_name', '127.0.0.1'), ], ]
然后在auth.php
中添加它们:
'guards' => [ [...] 'global' => [ 'driver' => 'session', 'provider' => 'globals', ], 'tennant' => [ 'driver' => 'session', 'provider' => 'tennants', ], ], [...] 'providers' => [ [...] 'globals' => [ 'driver' => 'eloquent', 'model' => App\UserGlobal::class, ], 'tennants' => [ 'driver' => 'eloquent', 'model' => App\UserTennant::class, ], ],
在每个可验证的模型中定义受保护的$connection = 'connection name'
,最后在模型中使用:
protected $connection = 'connectionname';