Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Fatal Error Class Not Found when migrating

  1. I've run artisan migrate:reset.

  2. I've deleted some of my migration files because I didn't need these tables anymore.

  3. I ran composer dump-autoload followed by artisan dump-autoload

  4. I ran artisan migrate and I keep getting this error:

    PHP Fatal error: Class 'Foo' not found in /vagrant/LaravelBackend/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 297

I tried to:

  • Run again composer dump-autoload and artisan dump-autoload (also used artisan clear-compiled)

  • Remove the migration table and run artisan migrate:install

  • Remove the vendor and composer.lock file and run composer install

  • Search within my project with PHPStorm for class Foo. Didn't find anything.

    I keep getting the same error. It's the first time I run this since I updated to 4.2 if that could be related. Anything else I should be looking for?

like image 287
Wistar Avatar asked Jun 25 '14 16:06

Wistar


3 Answers

I had this problem too. Must remember: class name must be in accordance with filename. Simple file renaming helped me:)

For example: in file "2014_12_08_100923_create_items_tables.php" must be class with name "CreateItemsTables" with using CamelCase words.

like image 172
Juljan Avatar answered Oct 15 '22 19:10

Juljan


I solved my problem by

  1. Removing all migration
  2. Running composer dump-autoload
  3. Adding them back one by one and running php artisan migrate
  4. Deleting those that caused Laravel to throw an error
  5. Create new migrations to replace the deleted ones

I'm not sure why that worked but my guess is that I might have modified the class name of these problematic migrations in the past.

I also found that renaming the migration with its initial name (The one throwed with the fatal error) also works for some of them.

like image 44
Wistar Avatar answered Oct 15 '22 18:10

Wistar


I ran into this aswell and the solution was different to all of the above. The reason it was failing was because the filename was still mentioned in the migrations table of the DB. Because there were no unique columns I couldn't remove it with PHPMyAdmin and had to tak the CLI route.

Login to your server as root. Type the following:

mysql -p database_name

(it now prompts for your password. From here on out everything is preceded with mysql > which just means that you're in the Mysql environment.

select * from migrations;

Look for the migration file you removed and copy the name.

delete from migrations where migration = '2015_07_21_000119_create_some_table';

It should mention something about 1 row affected. Now verify that it's gone by typing the first command again:

select * from migrations;

If it's gone exit the Mysql environment by typing

exit;

Now try 'php artisan migrate:rollback' again and it should work like a charm :-)

like image 20
Stan Avatar answered Oct 15 '22 19:10

Stan