Arvind K.
Arvind K.

Reputation: 1304

Laravel env configuration not updating in terminal even after clearing all cache

I am not sure whether this issue is related to Laravel itself or about terminal bash/zsh in my MacBook, but I still need to fix this. Here is the issue.

I tried to run php artisan migrate when I first faced this issue. php artisan migrate showed Nothing to migrate., while I was sure there were new migrations.

I thought it would be an caching issue and hence I ran every possible command to try to clear Laravel environment variables configuration cache, including the following commands

php artisan config:cache
php artisan config:clear
php artisan cache:clear
php artisan optimize

But it did not help. I ran php artisan migrate and still Nothing to migrate..

Okay. Now, I ran tinker and then env('DB_DATABASE') to print DB_DATABASE environment variable using same terminal and it showed me database name which I used previously. I have changed DB_DATABASE value to other different versions in the meantime, after I used the one tinker is showing, and of course artisan is trying to connect to. So artisan is trying to use an old value and hence the issue.

I am attaching two screenshots depicting the issue.

Configuration File Screenshot

Terminal showing database even after clearing all cache

php artisan db also trying to connect to old database

Edit: I forgot to mention that code-wise, Laravel connects well to the "in-use" database marked as "Active" in the first screenshot.

Environment:

Laravel v8.83.27 (PHP v8.2.26)
OS: MacOS Monterey 12.7.6

Upvotes: 0

Views: 83

Answers (2)

Hans Paul
Hans Paul

Reputation: 21

When I had this same problem with env, I read that is better to use 'config' instead of 'env', I created my own config file (beacuse realy I need it), I used this new config file and worked fine.

Upvotes: 0

Mohammad Salehnia
Mohammad Salehnia

Reputation: 44

check config/database.php file

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

For example, if you hard-code the database configuration, it won't be read from the .env file anymore.

Upvotes: 0

Related Questions