如何在Laravel的auth.php(config/auth.php)中更改資料庫?我正在使用多個資料庫,希望將用戶儲存在另一個資料庫中。
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';