在laravel迁移中指定连接

Specify connection in a laravel migration

我正在为Laravel 6项目创建迁移。它具有多个数据库连接以确保数据安全。

我正在尝试使用laravel迁移来控制数据库和种子。为了使模型更清晰,我建立了两个在config/database.php

中定义的数据库

1
2
3
4
5
6
7
'connections' => [

    'core' => [ ... ],

    'regional' => [ ... ]

]

然后使用.env文件填充这些文件。这似乎按预期方式工作。

在开始进行迁移时,基本的laravel ...create_users_table.php文件具有以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

如何指定此数据库使用的连接?

预先感谢。


尝试Schema::connection('core')->create...

在此处查看更多信息:https://laravel.com/docs/6.x/migrations