10

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: Database Confict Error

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 .envfile of another project?

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47

2 Answers2

4

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'),
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Do you know why the .env file would not be read in preference to the config/database.php file? – kerrin Oct 05 '17 at 12:24
  • 1
    @kerrin Good question - I added some info. – Don't Panic Oct 05 '17 at 12:48
  • @Don'tPanic: sorry `env('DB_DATABASE', 'database_project_2')` was typing mistake. It was just an example name of db. But can be issue with cache or reading `.env` file – Pankaj Makwana Oct 05 '17 at 12:57
  • 1
    @PankajMakwana did this answer solved your issue? because it isn't working for me, I had to do a workaround for this i.e define `protected $table='DB1_Name.TABLE_NAME` without this, project 1 is using project 2 DB name @Don'tPanic any idea how can I fix this, I have cleared all caches already – Pankaj Jha Oct 29 '18 at 18:19
  • @PankajJha I would suggest opening a new question with clear details - there was some confusion and typos in the OP's question. Also, [clearly writing out the question as if explaining it to someone](https://meta.stackexchange.com/questions/42161/has-anyone-found-an-answer-to-a-question-using-stack-overflow-without-actually-p) can help! :-) – Don't Panic Oct 29 '18 at 18:25
  • 2
    @Don'tPanic thanks for the pointer. finally, my issue was resolved because of [this answer](https://stackoverflow.com/questions/43789681/two-laravel-projects-on-same-server-causing-conflict) I updated my project 1 env file `DB_DATABASE` to `DB_DATABASE1` and updated same in its config file. – Pankaj Jha Oct 29 '18 at 18:44
1

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
Robin
  • 51
  • 2