Class 'App\models\Test' not found when try to access model

Class 'App\models\Test' not found when try to access model

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Class 'App\models\Test' not found: Diagnosing Laravel Autoload Errors

When you encounter the FatalErrorException stating Class 'App\models\Test' not found, the error message is often more confusing than it needs to be. It explicitly references a class path that includes backslashes (\) rather than forward slashes, which immediately signals a namespace resolution issue within your application's autoloader.

In modern Laravel versions, the directory structure for models has shifted significantly from early iterations of the framework. The standard convention places models in the app/Models directory (plural, PascalCase), not app/models. However, even if you strictly adhere to legacy conventions and place your file at app/models/Test.php, the PHP autoloader will still fail to map the namespace correctly unless specific conditions are met.

The core issue lies in how PHP constructs fully qualified class names using namespaces. In your controller, you have imported the model with:

use App\models\Test;

You are using forward slashes (/) inside the use statement. While this syntax is valid for autoloading files if the file path matches exactly (i.e., app/models/Test.php), it creates a mismatch with standard namespace conventions. Standard namespaces use backslashes (\). If your file is located at app/models/Test.php, PHP expects the namespace declaration inside that file to match the directory structure using backslashes, or you must ensure the autoloader can map the string literal to the file.

However, the most common cause of this specific error in modern Laravel projects is a mismatch between the file location and the expected namespace path. If your file is at app/models/Test.php, the namespace inside that file should ideally be declared as:

namespace App\Models; // Note the backslash

If you declare it as \App\models\Test (using forward slashes in the namespace declaration) inside a file located at app/models/Test.php, PHP interprets this differently depending on your Laravel version. In Laravel 10 and later, the framework enforces strict naming conventions where models reside in app/Models. If you are trying to force an old-style path, the autoloader might not register it correctly if the directory structure does not align with the PSR-4 standard.

The error message specifically mentions App\models\Test. This indicates that PHP is looking for a class defined in the namespace App within a subdirectory named models, but the internal logic or the autoloader mapping might be failing to resolve the string to the physical file.

Verifying Autoload Configuration

You mentioned running composer dump-autoload. This command regenerates the autoload files (vendor/composer/autoload_classmap.php and vendor/composer/autoload_namespaces.php). While this is a necessary step after changing directory structures, it does not always fix namespace declaration errors.

If your file exists at app/models/Test.php, you must ensure the namespace inside that file matches the directory path using backslashes:

<?php

namespace App\Models; // Must use backslash for PSR-4 compliance in modern contexts

use Illuminate\Database\Eloquent\Model;

class Test extends Model {
    protected $table = 'users';

    public static function getMyData() {
        // Avoid calling find() without arguments as it returns a Collection of all records
        // or throws an exception depending on configuration. Use find($id) for specific lookups.
    }
}

If you are on an older version of Laravel (pre-10) and your file is strictly at app/models/Test.php, the namespace declaration inside must be namespace App\Models; (using backslashes), but the directory name remains models. The confusion often arises because developers mix up the physical file path separators with namespace separators.

Troubleshooting Steps

If you have corrected the namespace declaration to use backslashes inside the file and ensured the file exists, clear your application cache to ensure the changes are picked up:

php artisan config:clear
php artisan route:clear
php artisan view:clear
composer dump-autoload --optimize

The --optimize flag ensures that the autoloader generates an optimized class map, which can speed up loading and sometimes resolves stale references. If the error persists after these steps, inspect the vendor/composer/autoload_classmap.php file directly to see if your class is being registered. You might find that the key in the array uses forward slashes if the namespace declaration inside the file was incorrect, or vice versa.

Finally, consider upgrading your model structure to match current Laravel standards. Moving the file to app/Models/Test.php and updating the import statement to:

use App\Models\Test;

is often the cleanest solution. This aligns with PSR-4 autoloading best practices, ensuring that TestController can reliably resolve the class without ambiguity.

Our network