普通网友 2016-09-21 16:54
浏览 52

Laravel 5.3只读取.env文件中的db conf而不是config / database.php

I'm trying to make multiple DB connections and saw: http://fideloper.com/laravel-multiple-database-connections

i also try this

I set my DB config in config/database.php as:

'default' => 'web',

'connections' => array(

    # Our primary database connection
    'web' => array(
        'driver'    => 'mysql',
        'host' => env('DB_HOST', 'host1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'database1'),
        'username' => env('DB_USERNAME', 'user1'),
        'password' => env('DB_PASSWORD', 'pass1'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'another' => array(
        'driver'    => 'mysql',
        'host' => env('DB_HOST', 'host2'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'database2'),
        'username' => env('DB_USERNAME', 'user2'),
        'password' => env('DB_PASSWORD', 'pass2'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
),

I built my schema as well as follow:

public function up()
    {
        Schema::connection('another')->create('some_table', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->string('test')->unique();
            $table->string('one')->index();
            $table->timestamp('created_at');
        });
    }

But then when I want to run artisan migrate it sais:

[InvalidArgumentException]

Database [mysql] not configured.

I understand that mysql is read from .env file (because if I change it to something, the name changes).

I tried to change .env file as follow:

DB_CONNECTION=web
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=username
DB_PASSWORD=secret

Then I run artisan migrate again, it didn't throw me the error anymore, but it seems like the table is not created as I want. The table from my schema above was created in 'web' connection instead.

If you see my schema:

Schema::connection('another')->create('some_table', function (Blueprint $table)

That table should be created in 'another' connection. Not in web connection.

I also run:

artisan migrate:install --database=web
artisan migrate:install --database=another

as well as:

artisan migrate --database="web" --path="database/migrations/web"
artisan migrate --database="another" --path="database/migrations/another"

all of them only creating tables in web connection

basically it only read configuration in .env files. it never listen to my: config/database.php

What did I miss here?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog