为nWidart/laravel模块中的每个模块创建并使用自定义config/database.php

为nWidart/laravel模块中的每个模块创建并使用自定义config/database.php,database,laravel,module,configuration,Database,Laravel,Module,Configuration,在使用nWidart/laravel模块创建的每个模块中,都有一个Config目录,附带一个Config.php文件: <?php return [ 'name' => 'Contracts' ]; 在项目根目录下的.env文件中(因为我不知道如何为我的模块创建自定义环境),我添加了几行: DB_CONNECTION_MODULE1=module1 DB_MODULE1_DATABASE=module1database DB_MODULE1_USER

在使用nWidart/laravel模块创建的每个模块中,都有一个
Config
目录,附带一个
Config.php
文件:

<?php
    return [
        'name' => 'Contracts'
    ];
在项目根目录下的
.env
文件中(因为我不知道如何为我的模块创建自定义环境),我添加了几行:

DB_CONNECTION_MODULE1=module1
DB_MODULE1_DATABASE=module1database
DB_MODULE1_USERNAME=root
DB_MODULE1_PASSWORD=password
例如,我的一次迁移如下所示:

<?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateObjectivesTable extends Migration{

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up(){
            Schema::connection( env( 'DB_DATABASE_MODULE1' ) )->create( 'objectives', function( Blueprint $table ){
                $table->id();
                $table->text( 'description' );

                $table->timestamp( 'created_at' )->default( DB::raw( 'CURRENT_TIMESTAMP' ) );
                $table->timestamp( 'updated_at' )->default( DB::raw( 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP' ) );
            } );
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down(){
            Schema::connection( env( 'DB_DATABASE_MODULE1' ) )->dropIfExists( 'objectives' );
        }
    }

我想,我仍然需要做一些事情来阅读
Modules\Module1\Config\database.php
,如有任何帮助,将不胜感激。我还希望能够在模块内设置模块的环境(可能类似于
\Module1\.env
),这也将非常感谢。

迁移有一个
受保护的$connection
变量,您可以设置耶,但我想从\Modules\Module1\Config\database.php获取连接信息,如果我使用$connection,它仍然会在\config\database.php中查找连接数组,这是我不想做的。因此,它会将配置合并到主配置中。好的,您能告诉我如何从模块内部执行此操作吗?
<?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateObjectivesTable extends Migration{

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up(){
            Schema::connection( env( 'DB_DATABASE_MODULE1' ) )->create( 'objectives', function( Blueprint $table ){
                $table->id();
                $table->text( 'description' );

                $table->timestamp( 'created_at' )->default( DB::raw( 'CURRENT_TIMESTAMP' ) );
                $table->timestamp( 'updated_at' )->default( DB::raw( 'CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP' ) );
            } );
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down(){
            Schema::connection( env( 'DB_DATABASE_MODULE1' ) )->dropIfExists( 'objectives' );
        }
    }
Migrating: 2020_11_10_134237_create_objectives_table

    InvalidArgumentException

    Database connection [module1] not configured. 

at \path\to\my\site\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:152
     148▕         // If the configuration doesn't exist, we'll throw an exception and bail.
     149▕         $connections = $this->app['config']['database.connections'];
     150▕
     151▕         if (is_null($config = Arr::get($connections, $name))) {
  ➜ 152▕             throw new InvalidArgumentException("Database connection [{$name}] not configured.");
     153▕         }
     154▕
     155▕         return (new ConfigurationUrlParser)
     156▕                     ->parseConfiguration($config);

  1   \path\to\my\site\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:115
      Illuminate\Database\DatabaseManager::configuration("module1")

  2   \path\to\my\site\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php:86
      Illuminate\Database\DatabaseManager::makeConnection("module1")