Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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');
    }
}
like image 330
Federico Fia Sare Avatar asked Feb 19 '19 11:02

Federico Fia Sare


2 Answers

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.

like image 71
Jason Avatar answered Oct 20 '22 01:10

Jason


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.

like image 49
Mihir Bhende Avatar answered Oct 20 '22 01:10

Mihir Bhende