每个数据库使用一个 Laravel 迁移表

2023-10-31php开发问题
2

本文介绍了每个数据库使用一个 Laravel 迁移表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在一个使用多个数据库的项目中工作.Laravel 似乎只使用数据库中默认设置的迁移表.我希望每个数据库都有一个迁移表,用于记录对该特定数据库所做的迁移.这可能吗?

I work in a project that uses multiple databases. It seems like Laravel only uses the migrations-table in the database that is set as default. I would like one migrations table per database that logs the migrations that have been done to that specific database. Is this possible?

我在配置中定义了这样的数据库:

I have defined the databases in the config like this:

'connections' => [
    'db1' => array(
        'driver'    => 'mysql',
        'host'      => 'db1.host',
        'database'  => 'db1',
        'username'  => 'username',
        'password'  => 'password',
    ),
    'db2' => [
        'driver'    => 'mysql',
        'host'      => 'db2.host',
        'database'  => 'db2',
        'username'  => 'username',
        'password'  => 'password',
    ]
],

我还将第一个数据库 (db1) 设为默认数据库

I also made the first database (db1) the default one

'default' => 'db1'

我在两个数据库上都安装了迁移表

I install the migrations table on both databases

artisan migrate:install --database=db1
artisan migrate:install --database=db2

之后我继续创建几个数据库特定的迁移

After that i proceed to create a couple of database specifc migrations

在db1数据库中创建表test1:

Create table test1 in db1 database:

use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateTest1Table extends Migration
{
    public function up()
    {
        Schema::connection('db1')->create('test1', function(Blueprint $table)
        {
            $table->increments('id')->unsigned();
        });
    }

    public function down()
    {
        Schema::connection('db1')->drop('test1');
    }
}

在db2数据库中创建表test2:

Create table test2 in db2 database:

use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateTest2Table extends Migration
{
    public function up()
    {
        Schema::connection('db2')->create('test2', function(Blueprint $table)
        {
            $table->increments('id')->unsigned();
        });
    }

    public function down()
    {
        Schema::connection('db2')->drop('test2');
    }
}

我现在运行迁移

artisan migrate

预期结果

db1.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test1_table_in_db1   |     1 |
+-----------------------------+-------+

db2.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test2_table_in_db2   |     1 |
+-----------------------------+-------+

实际结果

db1.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test1_table_in_db1   |     1 |
| create_test2_table_in_db2   |     1 |
+-----------------------------+-------+

db2.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
Empty set

推荐答案

关于在 Laravel 中使用多个数据库的文章 -https://stackcoder.in/posts/laravel-7x-multiple-database-connections-migrations-relationships-querying

--database 参数与 migrate 命令一起使用,并将每个数据库的迁移存储在单独的目录中.

Use the --database parameter with the migrate command and store the migrations for each database in separate directories.

您可以在 app/database/migrations 中为每个数据库(在您的情况下为 db1db2)设置单独的目录并存储每个目录中的适当迁移.然后你可以像这样运行迁移:

You could have separate directories in app/database/migrations for each of your database (in your case db1 and db2) and store the appropriate migrations in each directory. Then you could run the migrations like this:

artisan migrate --database="db1" --path="app/database/migrations/db1"
artisan migrate --database="db2" --path="app/database/migrations/db2"

这样,您的 migrations 表将独立于每个数据库.

This way your migrations table will be independent for each database.

如果您想加倍努力并自动化该过程,您可以创建自定义命令来一次运行所有迁移.您可以像这样创建命令(对于 Laravel 5.0 到 5.2 使用 make:console 或对于 Laravel 5.2+ 使用 make:command):

If you want to go the extra mile and automate the process you could create your custom command that will run all the migrations at once. You can create the command like this (use make:console for Laravel 5.0 up to 5.2 or make:command for Laravel 5.2+):

artisan command:make MigrateAllCommand --command=migrate:all

这将创建一个新文件 app/commands/MigrateAllCommand.php.您的命令的 fire 方法如下所示:

This will create a new file app/commands/MigrateAllCommand.php. Your command's fire method would look something like this:

public function fire()
{
    foreach (Config::get('database.connections') as $name => $details)
    {
        $this->info('Running migration for "' . $name . '"');
        $this->call('migrate', array('--database' => $name, '--path' => 'app/database/migrations/' . $name));
    }
}

只要数据库配置键的名称与迁移目录名称相同,这将起作用.然后你可以这样称呼它:

This will work provided the name of the database configuration key is the same as the migration directory name. You can then just call it like this:

artisan migrate:all

您可以查看 Laravel 命令文档了解更多信息.

You can check the Laravel Command Docs for more info.

这篇关于每个数据库使用一个 Laravel 迁移表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17