简体   繁体   中英

Conflict database when Multiple Laravel Projects on single machine or laravel not reading .env file

I am using xampp on windows 10. I have multiple laravel 5.2 projects on this machine. When I am executing Project 1 it gives me the error that database_project_1.table_of_project_2 table or view do not exist, but the table table_of_project_2 is existing in the database_project_2 . This issue comes rarely.

Below is the Project 1 .env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:ratSluNv930gb3wp1UOabW6Ze3jEJn3ixtTX/wgqYZc=
APP_URL=http://project-1.dev/

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_project_1
DB_USERNAME=root
DB_PASSWORD=j@yshr33r@m

Below is the Project 2 .env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:XRgQHfYiKPmHtHZ5UbX38KDlBnl/nyBSt+8qnkOISTg=
APP_URL=http://project-2.dev/

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_project_2
DB_USERNAME=root
DB_PASSWORD=j@yshr33r@m

I have tried below commands but with no luck:

  1. php artisan config:clear
  2. php artisan cache:clear

Please check the screenshot below: 数据库错误

Please let me know if any thing is missing.

Here is the config/database.php code for both projects.

Project 1 config/database.php

<?php

return [
    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'database_project_1'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'j@yshr33r@m'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

    ],
    'migrations' => 'migrations',
    'redis' => [
        'cluster' => false,
        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
];

Project 2 config/database.php

<?php

return [
    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'database_project_2'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'j@yshr33r@m'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

    ],
    'migrations' => 'migrations',
    'redis' => [
        'cluster' => false,
        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
];

Take a look at the code and let me know anything required.

Is it problem with reading .env file or conflicting .env file of another project?

You have specified to use database_project_1 in Project 2's config/database.php :

'database' => env('DB_DATABASE', 'database_project_1'),

So if ever env('DB_DATABASE') does not return a value , the default of database_project_1 will be used. That can happen depending on caching, as described in a few questions here on SO: example 1 , example 2 , example 3 .

If it is a caching issue, you can try to fix that by some combination of the following (varies between installs and versions):

php artisan config:clear
php artisan cache:clear
composer dump-autoload
// restart your web server

But surely the simplest fix would be to just use the correct default value in your Project 2 config/database.php :

'database' => env('DB_DATABASE', 'database_project_2'),

On production we should use cache for config and should not use env('xxx') at runtime. If you like to use env function, I suggest that use different name for each project

DB_DATABASE_PROJECT1=pro1
DB_USERNAME_PROJECT1=user1
DB_PASSWORD_PROJECT1=xxx

and

'database' => env('DB_DATABASE_PROJECT1', 'project_1'),
...

cache

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:cache
php artisan route:cache

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM