Laravel migrations: Class "not found"
PhpSymfonyLaravelAzurePhp Problem Overview
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');
}
}
Php Solutions
Solution 1 - Php
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.
Solution 2 - Php
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
If you are using Laravel 9, there is no need for such issue since the migration class are being represented like so:
return new class extends Migration
Solution 3 - Php
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
Solution 4 - Php
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.
Solution 5 - Php
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.
Solution 6 - Php
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.
Solution 7 - Php
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.
Solution 8 - Php
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.
Solution 9 - Php
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
Solution 10 - Php
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
Solution 11 - Php
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.
Solution 12 - Php
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 simply create the file it depends of what u want, in my case I wanted to get rid of this migration. :)
Solution 13 - Php
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 :]
Solution 14 - Php
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.
Solution 15 - Php
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
Solution 16 - Php
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.
Solution 17 - Php
For anyone stumbling across this coming from Symfony running bin/console doctrine:migrations:execute [UNIX_TIMESTAMP] --down
:
The syntax has now changed from:
bin/console doctrine:migrations:execute 20220216142804 --down
To:
bin/console doctrine:migrations:execute 'DoctrineMigrations\Version20220216142804' --down
Where now you have to give the FQDN (sans the first backslash) in quotes instead of just the auto-generated timestamp from the classname like before.
Solution 18 - Php
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.