Laravel Error Message : Cannot declare class Class Name, because the name is already in use

Posted on

This is an error happened upon executing a command for processing migration script file. The concept itself, the migration script file processing is a concept or not only just a concept but also the implementation offered in Laravel web-based application development to operate with tables represented by each of the migration script file. The file itself is not specified since the process of executing the migration script files are done by executing all of the migration script files located normally in folder ‘/database/migrations’.

All of the migration script files can be executed by typing the following command in the bash prompt :

php artisan migrate:refresh

Below is the output of the above command execution :

user@hostname:/var/www/html/laravel$ php artisan migrate:refresh
PHP Fatal error: Cannot declare class CreateCategoriesTable, because the name is already in use in /var/www/html/laravel-support-ticket/database/migrations/2017_11_02_151257_create_categories_table.php on line 31

[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot declare class CreateCategoriesTable, because the name is already in use

user@hostname:/var/www/html/laravel$

In this article context, especially based on the above output generated by executing the commad php artisan migrate:refresh, the process cannot be proceed since it is actually generated an error message as shown in the above output. The error message is actually : “Cannot declare class CreateCategoriesTable, because the name is already in use”.

In the above context, “CreateCategoriesTable” is actually the name of the class specified in the migartion script file. It said that the class name has already exist because in the term of this case, there are three files which has the same name part which in the end there are three migration script files which have the same class name. Although those files are generated in the different time but it actually refers to the same class name. The situation is shown in the following :

user@hostname:/var/www/html/laravel/database/migrations$ ls -al
total 16
drwxrwxr-x 2 user     user 4096 Nov 5 22:08 .
drwxrwxr-x 3 www-data user 4096 Nov 5 22:22 ..
...
-rw-rw-r-- 1 www-data user 601 Nov 2 22:06 2017_11_02_150646_create_categories_table.php
-rw-rw-r-- 1 www-data user 601 Nov 2 22:12 2017_11_02_151257_create_categories_table.php
-rw-rw-r-- 1 www-data user 601 Nov 2 22:19 2017_11_02_151921_create_categories_table.php
...
user@hostname:/var/www/html/laravel/database/migrations$

In the context of this article, the solution which I implemented to solve the problem is by moving away the other files unnecessary. Since it can only be one script representing one class, just move the rest of the files into another location. I created one folder in the current path shown above and move the other two older files inside the folder. It is shown as follows :

user@hostname:/var/www/html/laravel/database/migrations$ mkdir temp
user@hostname:/var/www/html/laravel/database/migrations$

And move those files unrelated :

user@hostname:/var/www/html/laravel/database/migrations$ mv 2017_11_02_150646_create_categories_table.php 2017_11_02_151257_create_categories_table.php temp/
user@hostname:/var/www/html/laravel/database/migrations$ cd temp
user@hostname:/var/www/html/laravel/database/migrations$ ls -al 
total 16
drwxrwxr-x 2 user     user 4096 Nov 5 22:08 .
drwxrwxr-x 3 www-data user 4096 Nov 5 22:22 ..
-rw-rw-r-- 1 www-data user  601 Nov 2 22:06 2017_11_02_150646_create_categories_table.php
-rw-rw-r-- 1 www-data user  601 Nov 2 22:12 2017_11_02_151257_create_categories_table.php
user@hostname:/var/www/html/laravel/database/migrations/temp#

 

One thought on “Laravel Error Message : Cannot declare class Class Name, because the name is already in use

Leave a Reply