最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

caching - Laravel env configuration not updating in terminal even after clearing all cache - Stack Overflow

programmeradmin0浏览0评论

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.

Edit: I fot 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

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.

Edit: I fot 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

Share Improve this question edited Feb 8 at 20:07 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Feb 3 at 6:28 Arvind K.Arvind K. 1,3042 gold badges17 silver badges27 bronze badges 4
  • Environment variable can be forced by the server. in your case it can be forced by whatever is serving you app or/and most likely your system environment variables. – N69S Commented Feb 3 at 8:14
  • One tip I can give you is that, if you truly had the config cached, env(ANYTHING) would return null, so it is 100% not cached. I am thinking that you may have something in your OS setting those variables, printing your terminal environment variables would help a lot – matiaslauriti Commented Feb 3 at 9:10
  • Laravel Tinker does not know about changes you made in your code after it was started. You should use env('DB_DATABASE') via a route to see the current value. or be sure to exit and restart tinker – Emeka Mbah Commented Feb 3 at 9:21
  • debug config(), not env() – A. El-zahaby Commented Feb 4 at 23:39
Add a comment  | 

2 Answers 2

Reset to default 0

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', 'fe'),
        'username' => env('DB_USERNAME', 'fe'),
        '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.

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.

发布评论

评论列表(0)

  1. 暂无评论