Autoloading Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

Composer Autoload Error in Laravel - How to Fix

This error occurs when Composer's autoloader cannot find classes, usually after installing packages or moving files.

The Error

Error Message
Class not found after composer install

Common Causes

  1. 1 Autoload files not regenerated after changes
  2. 2 Namespace doesn't match directory structure
  3. 3 Package not properly installed
  4. 4 PSR-4 configuration incorrect in composer.json
  5. 5 Cached autoloader using old paths

Solutions

1

Regenerate autoload files

Bash
# Basic regeneration
composer dump-autoload

# Optimized for production
composer dump-autoload -o

# Clear everything and reinstall
rm -rf vendor
composer install
2

Check PSR-4 in composer.json

JSON
{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    }
}
3

Add custom namespace

JSON
// In composer.json, add your custom namespace
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "modules/"
    }
}

// Then run
composer dump-autoload
4

Clear Laravel caches

Bash
php artisan clear-compiled
php artisan cache:clear
php artisan config:clear
composer dump-autoload

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz

Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio

Related Errors