Error migrations: Cannot declare class X, because the name is already in use

PhpLaravelLaravel Migrations

Php Problem Overview


I do not know why this error occurs when I execute the migrations as I do not have repeated classes.

Migrations:

2014_10_12_100000_create_password_resets_table.php
2019_01_18_020910_create_roles_table.php
2019_01_18_025535_create_members_table.php
2019_01_18_025536_create_users_table.php
2019_01_18_183649_create_projects_table.php
2019_01_18_184249_create_member_project_table.php
2019_01_18_184719_create_sprints_table.php
2019_01_18_185218_create_tasks_table.php
2019_01_21_033045_add_shortname_to_project.php

Error:

PHP Fatal error:  Cannot declare class CreateRolesTable, because the name is already in use in
oyectos\database\migrations\2019_01_18_020910_create_roles_table.php on line 33

In 2019_01_18_020910_create_roles_table.php line 33:

  Cannot declare class CreateRolesTable, because the name is already in use

Class:

class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',128)->unique();
            $table->string('description');
            $table->boolean('system');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('roles');
    }
}

Php Solutions


Solution 1 - Php

As well as other answers given, this error can also happen if the migration filename is not a snake-case version of the class name.

So a migration file 2019_01_18_020910_create_roles_table.php must contain the class CreateRolesTable. If it contains the class CreateRoleTable, with a missing s, then the "Cannot declare X..." error is thrown. I've found this on Laravel 8, and may apply to earlier versions.

It appears to happen because Laravel loads the migration file multiple times when the filename is misspelled, and the second time loading is when the exception is throw.

Solution 2 - Php

First Solution :

It seems like you have 2 migrations done at different time with essentially same name.

for example : 2019_01_18_020910_create_roles_table.php

and 2019_01_16_020910_create_roles_table.php

Laravel will convert this filename eliminating the date signature and Camel Casing the remaining text.

So both of these migration will have class CreateRolesTable even if the time signatures are different. Check if your migrations directory have such 2 files.

To check this run this from terminal in project root : grep -ri 'createrolestable' database/migrations

Second Solution :

Sometimes composer's internal class autoloading causes this issue. Do following to check if it resolves :

run composer install

Third Solution :

This is likely to be invalid but a same file should not have same class declaration 2 files by mistake.

Fourth Solution :

There might be a package you have installed which has a migration with same class name. To find run grep -ril 'createrolestable' vendor

If it shows any file then thats what causing 2 classes to have same names.

You can create a new one php artisan make:migration create_roles_table_custom . and then copy what you have in current migration to the new one and delete the existing one(not from package but the one you have created).

This will create a class CreateRolesTableCustom which is different than what the package already has.

Solution 3 - Php

Becareful of migration file name.

For me, migration file name was:

2021-10-13_000000_create_examples_table

But correct was :

2021_10_13_000000_create_examples_table

LOL

Solution 4 - Php

If you are using Laravel 8 or above, you can use Anonymous Migration to avoid the conflict with Class name.

Below is how you to declare an Anonymous Migration. Do not forget the semicolon at the end.

return new class extends Migration
{
    //
};

More from the Docs.

Solution 5 - Php

In my case, i had my own package, which had migraration and it wasn't named properly. I named it without date like this: create_orders_table. I changed it to 2021_08_03_000000_create_orders_table and it helped.

Solution 6 - Php

I ran into this (misleading) error and it turned out I accidently omitted the word Create from the migration class name.

Error: Cannot declare class FooTable, because the name is already in use

Incorrect: class FooTable extends Migration

Correct: class CreateFooTable extends Migration

Solution 7 - Php

For me, it was a problem with Laravel Sanctum (now built into Laravel 8). I had generated migrations via a package and somehow ended up with something in vendor\laravel\sanctum\database\migrations.

I ran php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" to persist it to standard migrations.

See here for details.

Solution 8 - Php

This can be caused by a number of things. Follow these steps to resolve the issue. First run this command in terminal

> php artisan optimize:clear

> composer dump-autoload

If those don't resolve the issue, then you have renamed a migration file that was published from Laravel Cashier. To resolve it, do the following:

> Rename the migration file. Something like 2019_01_18_020910_create_roles_table can be renamed to 2019_01_18_020910_create_role_table

> Rename the class. Something like CreateRolesTable can be renamed to CreateRoleTable

Solution 9 - Php

I had this problem. I used composer dump-autoload and it solved the problem.

Solution 10 - Php

Even if you don't have any such files with same Class name and you are still facing the same problem then try

composer dump-autoload

Solution 11 - Php

in my case, it's throwing the error because I changed the time of migration file which are added by laravel cashier, my time sequence is correct but it's still throwing name issue.

Then I revert the time back to the original migration time and the problem is solved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFederico Fia SareView Question on Stackoverflow
Solution 1 - PhpJasonView Answer on Stackoverflow
Solution 2 - PhpMihir BhendeView Answer on Stackoverflow
Solution 3 - PhpMojtaba MichaelView Answer on Stackoverflow
Solution 4 - PhpascsoftwView Answer on Stackoverflow
Solution 5 - PhpVladimir SadovnikovView Answer on Stackoverflow
Solution 6 - PhpTylerView Answer on Stackoverflow
Solution 7 - PhpVael VictusView Answer on Stackoverflow
Solution 8 - PhpDankyi Anno KwakuView Answer on Stackoverflow
Solution 9 - PhpNachman RosenView Answer on Stackoverflow
Solution 10 - PhpJay MomayaView Answer on Stackoverflow
Solution 11 - PhpHadiNiaziView Answer on Stackoverflow