70
votes

I am deploying a Laravel barebone project to Microsoft Azure, but whenever I try to execute php artisan migrate I get the error:

[2015-06-13 14:34:05] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class '' not found' in D:\home\site\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:328

Stack trace:

 #0 {main}  

What could be the problem? Thank you very much

-- edit --

Migration Class

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('name', 50);
            $table->string('surname', 50);
            $table->bigInteger('telephone');
            $table->string('email', 50)->unique();
            $table->string('username', 50)->unique();
            $table->string('password', 50);
            $table->boolean('active')->default(FALSE);
            $table->string('email_confirmation_code', 6);
            $table->enum('notify', ['y', 'n'])->default('y');
            $table->rememberToken();
            $table->timestamps();
            
            $table->index('username');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}
17
Could you please post your migration code?Joel Hinz
Just make sure you also follow the solution here stackoverflow.com/questions/24413929/…xatzistnr

17 Answers

110
votes

For PSR-4 Auto Loader Users (composer.json):

Keep the migrations folder inside classmap array and do not include it inside psr-4 object under autoload head. As migrations' main class Migrator doesn't support namespacing. For example;

"autoload": {
    "classmap": [
        "app/database/migrations"
    ],
    "psr-4": {
        "Acme\\controllers\\": "app/controllers"
    }
}

Then run:

php artisan clear-compiled 
php artisan optimize:clear
composer dump-autoload
php artisan optimize
  • First one clears all compiled autoload files.
  • Second clears Laravel caches (optional)
  • Third builds the autoloader for namespaced classes.
  • Fourth optimizes various parts of your Laravel app and builds the autoloader for non-namespaced classes.

From this time on wards, you will not have to do this again and any new migrations will work correctly.

70
votes

Simply make sure your migration filename is the same as your class name.

i.e:

If filename is:

xxx_151955_create_post_translations_table.php

Then class should be:

CreatePostTranslationsTable

38
votes

If you get the "Class not found error" when running migrations, please try running this command.

composer dump-autoload 

then re-issuing the migrate command. See more details in the offical site (#Running Migrations): http://laravel.com/docs/master/migrations#running-migrations

12
votes

I had this same problem a while back. Apparently it is a common problem because in the documentation for Laravel, it even suggests it: http://laravel.com/docs/master/migrations#running-migrations

Basically all you have to do is refresh some composer files. Simply run:

composer dump-autoload

This will refresh composer autoload files and then you can run your normal migration and it should work! Very best.

10
votes

I think it's late to answer this question but maybe this will help someone.

If you changed the migration file name, be sure about its inner class name.

For example, If I change a migration name from 2018_06_10_079999_create_admins_table.php to 2018_06_10_079999_create_managers_table.php so its inner class name must change from CreateAdminsTable to CreateManagerTable too.

9
votes

i also run in the same problem.

The solution for me was to delete the migration file, AND delete the record from the "migrations" table in the database.

After that, I ran

composer dump-autoload

and was finally able to reset/rollback migrations.

6
votes

I deleted one of the migration file. faced the same problem, while php artisan migrate:rollback

Then I tried composer dump-autoload. Again the same turned up.

I restored the deleted file and tried composer dump-autoload and php artisan migrate:rollback. It works.

5
votes

For me, the problem was that I had named my migration 2017_12_15_012645_create_modules_problems.php, with the class name of CreateModulesProblemsTable. As soon as I added _table to the filename, everything worked fine.

4
votes

i face this problem when i rename migrations to :

0_create_activities_table.php

i solve it when i rename it to:

2019_10_01_0_create_activities_table.php

3
votes

I had a similar situation (class not found error) after moving a Laravel 5.2 dev project to production. The production server was looking for class "project" but the controller name was Project.php. Once I renamed the file to project.php it was good to go.

3
votes

Make sure your migrations filename matches with migration class name

FOR EXAMPLE:

If name of migration is:

2020_10_31_161839_create_notifications_table

Then the class name should be:

CreateNotificationsTable

2
votes

I was receiving the same class not found error when trying to migrate my project. Sometimes it is the simple things that get you. In my case, I noticed that my class name was not correct in my migration file due to me making a rename change early on and not carrying that change throughout.

After correcting the class name I performed a composer dump-autoload and my issue went away.

HTH someone :]

2
votes

simply delete the row at your database on table migrations and that will fix the problem. It will not show anymore when you do migrations

other way is to simple create the file it depends of what u want, in my case I wanted to get rid of this migration. :)

1
votes

I had foolishly put:

namespace database\migrations;

Inside my migration create_users_table.php [2014_10_12_000000_create_users_table.php]

I was getting a similar error - Class 'CreateUsersTable' not found.

Removing this line at the top solved this error.

0
votes

Just make sure the following two files contain the correct class name and migration name :

C:\xampp\htdocs\StuffSpot\vendor\composer\autoload_classmap.php C:\xampp\htdocs\StuffSpot\vendor\composer\autoload_static.php

0
votes

I had a situation when I renamed one of the migration files and solved this problem, just in the migration table I also rewrote the record and everything worked.

-1
votes

In my case was the auto-increment of the database, In the past I did remove manually one entry, and AUTO_INCREMENT was pointing one more than the next id in the table.

Apparently Laravel uses AUTO_INCREMENT-1 to know which was the last migration done.