<bdo id='QwaFR'></bdo><ul id='QwaFR'></ul>
  • <small id='QwaFR'></small><noframes id='QwaFR'>

    <legend id='QwaFR'><style id='QwaFR'><dir id='QwaFR'><q id='QwaFR'></q></dir></style></legend>

      <i id='QwaFR'><tr id='QwaFR'><dt id='QwaFR'><q id='QwaFR'><span id='QwaFR'><b id='QwaFR'><form id='QwaFR'><ins id='QwaFR'></ins><ul id='QwaFR'></ul><sub id='QwaFR'></sub></form><legend id='QwaFR'></legend><bdo id='QwaFR'><pre id='QwaFR'><center id='QwaFR'></center></pre></bdo></b><th id='QwaFR'></th></span></q></dt></tr></i><div id='QwaFR'><tfoot id='QwaFR'></tfoot><dl id='QwaFR'><fieldset id='QwaFR'></fieldset></dl></div>

        <tfoot id='QwaFR'></tfoot>

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

        Use one Laravel migrations table per database(每个数据库使用一个 Laravel 迁移表)
        • <bdo id='bpzpE'></bdo><ul id='bpzpE'></ul>

          <legend id='bpzpE'><style id='bpzpE'><dir id='bpzpE'><q id='bpzpE'></q></dir></style></legend>
            <tbody id='bpzpE'></tbody>
            • <small id='bpzpE'></small><noframes id='bpzpE'>

              1. <tfoot id='bpzpE'></tfoot>

                  <i id='bpzpE'><tr id='bpzpE'><dt id='bpzpE'><q id='bpzpE'><span id='bpzpE'><b id='bpzpE'><form id='bpzpE'><ins id='bpzpE'></ins><ul id='bpzpE'></ul><sub id='bpzpE'></sub></form><legend id='bpzpE'></legend><bdo id='bpzpE'><pre id='bpzpE'><center id='bpzpE'></center></pre></bdo></b><th id='bpzpE'></th></span></q></dt></tr></i><div id='bpzpE'><tfoot id='bpzpE'></tfoot><dl id='bpzpE'><fieldset id='bpzpE'></fieldset></dl></div>
                1. 本文介绍了每个数据库使用一个 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 迁移表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Laravel 4 - Connect to other database(Laravel 4 - 连接到其他数据库)
                  Call external API function from controller, LARAVEL 4(从控制器调用外部 API 函数,LARAVEL 4)
                  Empty string instead of null values Eloquent(空字符串而不是空值 Eloquent)
                  quot;laravel.logquot; could not be opened: failed to open stream(“laravel.log无法打开:无法打开流)
                  Displaying the Error Messages in Laravel after being Redirected from controller(从控制器重定向后在 Laravel 中显示错误消息)
                  Laravel Creating Dynamic Routes to controllers from Mysql database(Laravel 从 Mysql 数据库创建到控制器的动态路由)

                  • <i id='oBLtj'><tr id='oBLtj'><dt id='oBLtj'><q id='oBLtj'><span id='oBLtj'><b id='oBLtj'><form id='oBLtj'><ins id='oBLtj'></ins><ul id='oBLtj'></ul><sub id='oBLtj'></sub></form><legend id='oBLtj'></legend><bdo id='oBLtj'><pre id='oBLtj'><center id='oBLtj'></center></pre></bdo></b><th id='oBLtj'></th></span></q></dt></tr></i><div id='oBLtj'><tfoot id='oBLtj'></tfoot><dl id='oBLtj'><fieldset id='oBLtj'></fieldset></dl></div>
                      • <bdo id='oBLtj'></bdo><ul id='oBLtj'></ul>

                        <small id='oBLtj'></small><noframes id='oBLtj'>

                        <legend id='oBLtj'><style id='oBLtj'><dir id='oBLtj'><q id='oBLtj'></q></dir></style></legend>
                          <tbody id='oBLtj'></tbody>
                        <tfoot id='oBLtj'></tfoot>