How to fix "cannot declare class because the name is already in use" error in Laravel/PHP

Published under Laravel.

"cannot declare class because the name is already in use"

This can be an annoying error to track down.

Quick fix

The first thing to look for is that you do not have more than one of the same class defined in the namespace.

If that doesn't solve your problem, then you need to start hunting. Make sure you clear your compiled cache too:

Checking vendor files

In my case, I had renamed a Laravel migration file that was published from Laravel Cashier. This turned out to be a problem. Here's how it breaks down:

So now we have an issue. Both migration files (my own codebase, and Laravel Cashier's version), have different file names, but they both still have the same class name: CreateCustomerColumns.

The fix?

You have two options:

  • Don't rename the original migration files

  • If you must rename the migration, also make sure to rename the class too!

Thanks for reading!

Did you find this post useful? Let me know on Twitter! If you found an issue with the content, submit a pull request!

Subscribe to my newsletter to know when I publish more content in the future.

❤️ Likes: 0
📣 Retweets: 0
💬 Replies: 0
🙊 Mentions: 0