Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CookieMonster's avatar

php artisan config:cache removes all tables

I am running unit tests and all the tests passed but when I run php artisan config:cache, running the test again makes most of my test failed and I checked my database, the tables were gone.

Why does this happen and how do I fix this?

Tried to run php artisan migrate and run the phpunit test again and the tables are removed again.

0 likes
11 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Never cache your config on testing/dev environments.

run php artisan config:clear and try again

And I am fairly confident that caching your config, does not remove tables from your database :)

1 like
Sinnbeck's avatar

Well I would need to know a bit more about your setup then. Caching will not remove tables (it just adds a php file with your configuration).

  1. What database are you using for testing?
  2. What exact database is it removing tables from
  3. Does it happen when you cache config, or when you run tests after caching config (more likely)

And again.. Dont cache your config :)

CookieMonster's avatar

@sinnbeck How come running config:clear will make it work again?

  1. I am using mysql workbench for database. For testing, I configured a local test on my configuration.

  2. It removes all the tables of migration that I have created so I am not too sure.

  3. It happens when i run config:cache and I noticed the tables in my db are all missing. Running the vendor/bin/phpunit then removes it again.

Sinnbeck's avatar

It will work as you config isnt cached. It shouldnt be. If the config is cached you disable checking environment variables, causing testing fail.

Most likely your tests are set to cleanup after themself. When you cached your config, your testing didnt know to use the testing database (as environment is disabled), and it therefor use your "real" database.

CookieMonster's avatar

@sinnbeck Okay it makes sense. So avoid caching config when using phpunit.

But it's still odd that it removes the actual tables from my database though.

GeordieJackson's avatar

@nickywan123

Are you using the use RefreshDatabase trait in your test?

This will remove all the database tables after the test.

If that's the problem, try using use DatabaseMigrations instead of use RefreshDatabase

CookieMonster's avatar

@sinnbeck Thanks, but i can assure you I didn't use RefreshDatabase trait, I only used DatabaseMigrations trait.

Sinnbeck's avatar

@nickywan123 Ok. Well something in your tests is changing your tables :) Without looking over your files it will be hard to say what. But anyways. Just make sure you dont cache config again, and all is good :)

Please or to participate in this conversation.