I’m currently refactoring my Laravel application to follow a more domain-driven design, and I’ve moved my User model to a new directory. However, now I keep getting the error "Class '\App\User' not found" whenever I try to authenticate or run migrations. I updated the namespace inside the model file itself, but it seems like something in the core configuration or the authentication provider is still looking for the old path. How do I globally update the User class reference so the framework recognizes the change?
3 answers
This error usually stems from the fact that Laravel’s authentication system and several configuration files have hardcoded references to the App\User namespace. To fix this, you must first update the model key in your config/auth.php file under the providers section to point to your new namespace, like App\Models\User. After that, check your app/Http/Controllers/Auth files if you are using Laravel UI or Breeze, as they often contain manual imports. Most importantly, you must run composer dump-autoload in your terminal to refresh the class map. If you are using Laravel 8 or higher, remember that the default location moved from App\User to App\Models\User, so ensure your factory and seeder namespaces are also updated accordingly.
Did you remember to check your config/services.php or any third-party packages like Laravel Socialite? I’ve found that even if you fix the main auth provider, these services often still have the old App\User string stored in their configuration arrays, causing the app to crash only during specific login events.
You likely just need to run php artisan config:clear and composer dump-autoload. Laravel caches your configuration files, so it might still be reading the old namespace from the cache.
I agree with Brandon. Clearing the cache is a vital step. I’d also recommend checking your composer.json file’s autoload section just in case you changed the root namespace of the entire app/ directory, as that requires a full re-optimization of the class map to function.
Joshua, that was exactly what I missed! I spent an hour debugging my auth.php only to realize my Stripe integration in services.php was still pointing to the old directory. For anyone else seeing this, a quick way to find all these hidden references is to do a global search in your IDE (Ctrl+Shift+F) for the string "App\User" and replace it with your new namespace. This ensures you don't miss any obscure config files or event listeners that might be listening for the old model path.