我在Laravel 5.7上遇到问题,它无法识别第二个数据库连接。数据库2有一个“国家”表,而数据库1没有。但是laravel坚持与连接数据库1。这是错误和实际代码,请帮忙!预先感谢
错误
SQLSTATE [42S02]:未找到基表或视图:1146表 'database_1.countries'不存在(SQL:从国家/地区中选择*)
模型
class Country extends Model {
use SoftDeletes;
protected $connection = 'mysql_2';
protected $table = 'countries';
protected $fillable = ['...'];
}
CONTROLLER / DEBUGGINB
class CountriesController extends Controller {
public function index(){
// neither works, this
$countries = DB::connection("mysql_2")->select('select * from countries');
// or this
$countries = Country::all();
dd($countries); --> both give ERROR
}
DATABASE.PHP
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_1'), //forge
'username' => env('DB_USERNAME', 'root'), // forge
'password' => env('DB_PASSWORD', ''),
.......
],
'mysql_2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_2'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
.......
],
.ENV文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql_2
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_2
DB_USERNAME=root
DB_PASSWORD=
并且CACHE已更新:
php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
答案 0 :(得分:2)
在您的database.php文件中尝试更改
'mysql_2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE2', 'database_2'), // THIS IS THE ONE THATS CHANGED
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
.......
],
.ENV文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql_2
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE2=database_2 // We change 'DB_Database' to 'DB_Database2'
DB_USERNAME=root
DB_PASSWORD=
答案 1 :(得分:1)
您可以尝试的另一个过程-
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_1
DB_USERNAME=root
DB_PASSWORD=
CUSTOM_DATABASE=database_2
CUSTOM_USERNAME=root
CUSTOM_PASSWORD=
database.php
'mysql' => [
'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', ''),
...................................
],
'custom' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('CUSTOM_DATABASE', 'forge'),
'username' => env('CUSTOM_USERNAME', 'forge'),
'password' => env('CUSTOM_PASSWORD', ''),
..........................................
],
YourModel.php
protected $connection = 'custom';
protected $fillable = [......];
如果在模型中使用protected $connection
,则可以照常使用雄辩的语言。如果是这样,则可以这样运行查询-
$countries = Country::all();
在您的迁移文件中-
public function up()
{
Schema::connection('custom')->create('your-table-name', function (Blueprint $table){
$table->bigIncrements('id');
...........................
$table->timestamps();
});
}