当一台机器上的多个Laravel项目或laravel无法读取.env文件时,冲突数据库 [英] Conflict database when Multiple Laravel Projects on single machine or laravel not reading .env file
问题描述
我在Windows 10上使用xampp.我在这台机器上有多个laravel 5.2项目.当我执行Project 1时,它给我一个错误,提示database_project_1.table_of_project_2
表或视图不存在,但database_project_2
中存在表table_of_project_2
.这个问题很少出现.
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.
下面是项目1 .env文件
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
下面是 Project 2 .env文件
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:
-
php artisan config:clear
-
php artisan cache:clear
php artisan config:clear
php artisan cache:clear
请检查以下屏幕截图:
Please check the screenshot below:
请让我知道是否缺少任何东西.
Please let me know if any thing is missing.
这是两个项目的config/database.php
代码.
Here is the config/database.php
code for both projects.
项目1 config/database.php
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,
],
],
];
项目2 config/database.php
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.
读取.env
文件或与另一个项目的.env
文件冲突是否有问题?
Is it problem with reading .env
file or conflicting .env
file of another project?
推荐答案
您已指定在Project 2的config/database.php
中使用database_project_1
:
You have specified to use database_project_1
in Project 2's config/database.php
:
'database' => env('DB_DATABASE', 'database_project_1'),
因此,如果env('DB_DATABASE')
不返回任何值,则将使用默认值database_project_1
.如缓存在SO上的几个问题所述,这可能取决于缓存:示例1 ,示例2 ,
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
但最简单的解决方法肯定是在Project 2 config/database.php
中使用正确的默认值:
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'),
这篇关于当一台机器上的多个Laravel项目或laravel无法读取.env文件时,冲突数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!