22
  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?

Community
  • 1
  • 1
Wistar
  • 3,770
  • 4
  • 45
  • 70
  • Did you try completely clearing your database, not just the migrations folder? – Laurence Jun 25 '14 at 16:25
  • Do any of your migrations "depend" on some of the other ones you deleted perhaps? Like adding a column to a table perhaps? – Laurence Jun 25 '14 at 16:26
  • @TheShiftExchange I remove every tables within the database. All my migrations are "independent", they do not require any other migration. Also, I recently updated to 4.2 if that could be involved... – Wistar Jun 25 '14 at 16:33
  • I had this problem once - and I cant remember how I solved it. If I was you - I would remove ALL your migrations - then add them in one at at a time, until the error returns. – Laurence Jun 25 '14 at 16:35
  • What is class `Foo`? Do you recognize this class? Is it something you wrote or a package you installed? This *might* have to do with items in the `providers` array in your `app/config/app.php` file - check to see that there's not something in there that shouldn't be. – Kryten Jun 25 '14 at 16:36
  • @Kryten I don't see anything obvious there but I'll dig. Thanks. – Wistar Jun 25 '14 at 16:44
  • Try again with `artisan clear-compiled` then `dump autoload` – Jarek Tkaczyk Jun 25 '14 at 16:48
  • @deczo I forgot to mentioned it but I've tried that as well – Wistar Jun 25 '14 at 17:06

11 Answers11

39

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.

Juljan
  • 2,391
  • 1
  • 17
  • 20
  • 1
    Everywhere you see the dump-autoload solution but no one explains this one. Thanks!! – keisar Jun 10 '15 at 08:00
  • 4
    Also you MUST follow the date formating prefix http://stackoverflow.com/questions/24413929/laravel-fatal-error-class-not-found-when-migrating/35265489#35265489 – xatzistnr Feb 08 '16 at 08:55
  • 2
    This is actually that answer (+ comment by @xatzisnr) that resolves the issue. The accepted answer is just a workaround (= start fresh / redo everything). You also likely need to `composer dump-autoload`, especially if you **renamed** you migration PHP file (manually). – eightyfive Mar 29 '16 at 07:42
27

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.

Mark
  • 317
  • 1
  • 3
  • 17
Wistar
  • 3,770
  • 4
  • 45
  • 70
  • 4
    Looks like you should now use `composer dump-autoload` and not `artisan`. (Laravel 5) – Nadav S. Jul 13 '15 at 06:37
  • 1
    Remember those filenames! They got to match. See Juljan's answer. – Captain Hypertext May 17 '16 at 20:54
  • works like a charm, though this should be addressed by Taylor Otwell and his team, I found the root of the issue and solution. This should be addressed in next patch: https://stackoverflow.com/a/31201694/6568457 – clusterBuddy Mar 21 '18 at 10:08
13

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

Stan
  • 131
  • 1
  • 3
  • This was my solution as well. The problem was that I had deleted a migration file, but the migration table still had a row for it, and `migrate:refresh` was trying to rollback the file. – QuickDanger Jul 29 '15 at 21:24
7

The actual solution is to use the correct naming for your translations. You still may need to do a

composer dump-autoload

The migration files must be as follows YYYY_MM_DD_000000_create_some_table.php and the class name inside must be

class CreateSomeTable extends Migration{}

xatzistnr
  • 174
  • 1
  • 7
  • This is what got me - I needed the "000000" in the filename and suddenly my error of "Class `SoftDelete` not found" went away and my migration worked perfectly. – Scott Salyer May 03 '22 at 19:01
4

There is an easier way.

  1. Recreate those delete migrations manually. artisan make:migration
  2. run artisan migrate:reset to rollback
  3. Delete those dummy migations files just created.
  4. run artisan migrate:refresh
John Mccandles
  • 1,181
  • 2
  • 16
  • 24
4

I know this is a bit past but there is a better way actually. Run the following in terminal and feel free yourself to remove any of them:

~$ php artisan clear-compiled;php artisan cache:clear;php artisan config:clear;php artisan debugbar:clear;php artisan view:clear;php artisan optimize

To do it a regular task create an executable file named artisan-clear:

#!/bin/bash

php artisan clear-compiled
php artisan cache:clear
php artisan config:clear
php artisan debugbar:clear
php artisan view:clear
php artisan optimize
YahyaE
  • 1,057
  • 1
  • 9
  • 24
1

For me, the solution was that my class name inside the migration somehow started with a lowercase letter. When I changed the class name to be all upper case, then ran a composer dump-autoload, it ended up working for me. This is using Laravel 5.1, for what it's worth.

Bono
  • 4,757
  • 6
  • 48
  • 77
Bay879
  • 11
  • 1
1

I did like this: 1. Deleted row non exist migration from migrations table from database 2. And run the command php artisan migrate:refresh

This helped to solve my problem.

*all your data will deleted from database tables

Abduhafiz
  • 3,318
  • 5
  • 38
  • 48
1

version 5.1.3 same problem fix it me:

  • drop database all tables
  • php artisan migrate:status

output: No migrations found. ok use it

  • php artisan migrate:install
  • php artisan migrate

output is:

Migrated: 2016_11_24_093015_dt_some_table
Migrated: 2016_12_05_141004_dt_some_table
Migrated: 2016_12_07_110518_dt_some_table
Migrated: 2016_12_08_141807_dt_some_table
Migrated: 2016_12_13_090832_dt_some_table

this problem is solved

Qh0stM4N
  • 182
  • 1
  • 9
0

I had the same problem. When I was hiting php artisan migrate:reset, I got Class 'CreateImagesTable' not found. And composer dump-autoload didn't help.

My solution was very easy:

  1. php artisan make:migration create_images_table --create=images
  2. composer dump-autoload
  3. Then I got: SQLSTATE[HY000]: General error: 1 no such table: images (SQL: drop table "images")
  4. so in sqlite I typed: CREATE TABLE `images` ( ...> `id` INTEGER ...> );
  5. And then php artisan migrate:reset
  6. Now I'm happy again
0

If artisan doesn't work at all and keeps throwing you this message no matter the command you give, delete the config.php file from bootstrap/cache folder.

After that run again

php artisan config:cache
Feralheart
  • 1,881
  • 5
  • 28
  • 59
Mihai Crăiță
  • 3,328
  • 3
  • 25
  • 37