Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Class App\User not found after moving the User model

ErrorClass "App\User" not found

Short 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.

  • Applies to Laravel 11, Laravel 12, PHP 8.3, Composer 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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

What does the error look like?

  • Class "App\User" not found
  • Class '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.php

2Fix

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:list

Confirmed by laravel.com

Did this fix it?

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.json

2Fix

Make the namespace match the folder (namespace App\Models for app/Models), then rebuild the autoloader.

namespace App\Models;

composer dump-autoload

3Verify

The class loads from the command line.

php artisan route:list

Confirmed by getcomposer.org

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.