Class App\User not found after moving the User model
Class "App\User" not foundShort answer
Newer Laravel apps keep the User model in app/Models, so its class is App\Models\User, not App\User. Code or config that still says App\User fails. Update the references, and when you move a class yourself, make the namespace match the folder and refresh Composer's autoloader.
Fix it fast
1Find out which cause you have
Find every place that still names the old class, and see where the real file is.
grep -rn "App\\\\User" app config database routes tests ls app/Models/User.php app/User.php2Match what you see, then jump to its fix
- 1
The file is app/Models/User.php, and the stack trace names a use statement or config line with App\User.Code still points at App\User, but the model is App\Models\UserFix - 2
The same not-found error names the new class, and the file is present in app/Models.You moved the class, but its namespace or autoload entry does not matchFix
- 1
What does the error look like?
Class "App\User" not foundClass 'App\User' not found
Cause 1 of 2
Code still points at App\User, but the model is App\Models\User
1Check
Since Laravel 8 the skeleton has an app/Models directory, and the generators assume models live there. The default auth config also names App\Models\User.
grep -rn "App\\\\User" app config database routes
grep -n "'model'" config/auth.php2Fix
Change the references to the new class name, including the model setting under providers in config/auth.php.
// config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
],
// anywhere else
use App\Models\User;3Verify
Clear caches and load the page that failed.
php artisan config:clear
php artisan route:listConfirmed by laravel.com
Cause 2 of 2
You moved the class, but its namespace or autoload entry does not match
1Check
Composer maps the App namespace to the app directory with PSR-4. The namespace line and the folder path must agree with that rule, and the autoloader file may be stale after moving files.
head -n 8 app/Models/User.php
grep -n -A3 "psr-4" composer.json2Fix
Make the namespace match the folder (namespace App\Models for app/Models), then rebuild the autoloader.
namespace App\Models;
composer dump-autoload3Verify
The class loads from the command line.
php artisan route:listConfirmed by getcomposer.org
How do I stop it coming back?
- Use the full class name in config files with ::class so a rename shows up in your editor.
- After moving or renaming a class, search the whole project for the old name, including config, seeders, factories and tests.
- Run composer dump-autoload after moving files between folders.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix