Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to run "composer dump-autoload" command to make migrations work in laravel?

I have built some migration classes in my application to create the tables I need, but I keep getting errors. I need to run this command:

composer dump-autoload

Only then it works again as expected. Am I doing something wrong that generates this error or this is a normal behaviour with migrations?

Below is the error that I get when running the migration process:

  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Class 'CreateVideoStatusTable' not found  
like image 825
Hasan Al-Natour Avatar asked Nov 28 '15 17:11

Hasan Al-Natour


People also ask

What is the use of composer dump-autoload command?

Composer remove: This command can be used to remove unused dependencies. You can uninstall such dependencies using the below command. Composer dump-autoload: The composer dump-autoload will not download any new thing, all it does is looking for all the classes and files it needs to include again.

What is the use of autoload in laravel?

Auto-Loading allows you to load class files when they are needed without explicitly loading or including them. This gives you ease in running your application by loading those files automatically which are needed every time. Laravel is built to work with Composer.

How do I undo a composer dump-autoload?

Yes, manually removing all files of the autoloader in the vendor/composer/ folder should work. So, all vendor/composer/autoload_*.


4 Answers

OK so I think i know the issue you're having.

Basically, because Composer can't see the migration files you are creating, you are having to run the dump-autoload command which won't download anything new, but looks for all of the classes it needs to include again. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php), and this is why your migration is working after you run that command.

How to fix it (possibly) You need to add some extra information to your composer.json file.

"autoload": {
    "classmap": [
        "PATH TO YOUR MIGRATIONS FOLDER"
    ],
}

You need to add the path to your migrations folder to the classmap array. Then run the following three commands...

php artisan clear-compiled 
composer dump-autoload
php artisan optimize

This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.

Ideally, you execute composer dump-autoload -o , for a faster load of your webpages. The only reason it is not default, is because it takes a bit longer to generate (but is only slightly noticable).

Hope you can manage to get this sorted, as its very annoying indeed :(

like image 95
Duenna Avatar answered Oct 09 '22 16:10

Duenna


You should run:

composer dump-autoload

and if does not work you should re-install composer

like image 25
afshindadashnezhad Avatar answered Oct 09 '22 17:10

afshindadashnezhad


Short answer: classmaps are static while PSR autoloading is dynamic.

If you don't want to use classmaps, use PSR autoloading instead.

like image 26
Daniel W. Avatar answered Oct 09 '22 17:10

Daniel W.


My error was placing a function inside config/fortify.php, in my case it was an anonymous function that gave the error.

The definitive solution for this is to see what files I normally modify in the config/ folder.

In many places they say delete the files from bootstrap/cache/* but it didn't work in my case, and it didn't really make sense. In any case I leave you the commands:

php artisan clear-compiled
composer dump-autoload
php artisan optimize

But I really recommend that you go through the base laravel config files that you have configured. here you will find the error.

like image 40
fafaaf Avatar answered Oct 09 '22 17:10

fafaaf