Home > Back-end >  Laravel 9 - Cannot declare class CreateUsersTable, because the name is already in use
Laravel 9 - Cannot declare class CreateUsersTable, because the name is already in use

Time:06-22

I deleted the users migration file that followed Laravel and created a new one. The blood has dried, I have been doing this for so long. The class name in the migration file was CreateUsersTable and I changed it to CreateUserTable. I deleted the database and from the beginning ... I cleared the cache of Laravel but... literarry literally nothing... Where does it cache it? Where is the information that 1-2 hours ago I had another migration file that I deleted but Laravel doesn't really care about it?

The error messages:

 Illuminate\Database\QueryException 

C:\xampp\htdocs\chikchika>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (9.05ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (11.17ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (24.01ms)
Migrating: 2022_06_21_083427_followers
Migrated:  2022_06_21_083427_followers (6.22ms)
Migrating: 2022_06_21_084259_email_verification
Migrated:  2022_06_21_084259_email_verification (6.40ms)
Migrating: 2022_06_21_084605_search_keywords
Migrated:  2022_06_21_084605_search_keywords (7.15ms)
Migrating: 2022_06_21_084834_dm_conversition
Migrated:  2022_06_21_084834_dm_conversition (7.65ms)
Migrating: 2022_06_21_085432_dm_messages
Migrated:  2022_06_21_085432_dm_messages (6.13ms)
Migrating: 2022_06_21_085828_posts
Migrated:  2022_06_21_085828_posts (5.89ms)
Migrating: 2022_06_21_090526_post_files    
Migrated:  2022_06_21_090526_post_files (6.21ms)
Migrating: 2022_06_21_090756_post_reactions
Migrated:  2022_06_21_090756_post_reactions (6.63ms)
Migrating: 2022_06_21_091042_comments
Migrated:  2022_06_21_091042_comments (7.98ms)
Migrating: 2022_06_21_092616_comment_reactions
Migrated:  2022_06_21_092616_comment_reactions (6.44ms)
Migrating: 2022_06_21_092807_system
Migrated:  2022_06_21_092807_system (11.89ms)
PHP Fatal error:  Cannot declare class CreateUsersTable, because the name is already in use in C:\xampp\
htdocs\chikchika\database\migrations\2022_06_21_124328_users.php on line 7

   Symfony\Component\ErrorHandler\Error\FatalError 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕
  ➜   7▕ class CreateUsersTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *


   Whoops\Exception\ErrorException 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕
  ➜   7▕ class CreateUsersTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *

  1   C:\xampp\htdocs\chikchika\vendor\filp\whoops\src\Whoops\Run.php:486
      Whoops\Run::handleError("Cannot declare class CreateUsersTable, because the name is already in use
", "C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php")

  2   [internal]:0
      Whoops\Run::handleShutdown()

C:\xampp\htdocs\chikchika>php artisan migrate
PHP Fatal error:  Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\h
tdocs\chikchika\database\migrations\2022_06_21_124328_users.php on line 7

   Symfony\Component\ErrorHandler\Error\FatalError 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;

C:\xampp\htdocs\chikchika>php artisan migrate
PHP Fatal error:  Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php on line 7

   Symfony\Component\ErrorHandler\Error\FatalError 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕ 
  ➜   7▕ class CreateUserTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *


   Whoops\Exception\ErrorException 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕ 
  ➜   7▕ class CreateUserTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.

C:\xampp\htdocs\chikchika>php artisan migrate
PHP Fatal error:  Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php on line 7

   Symfony\Component\ErrorHandler\Error\FatalError 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕ 
  ➜   7▕ class CreateUserTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *


   Whoops\Exception\ErrorException 

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

  at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7
      3▕ use Illuminate\Database\Migrations\Migration;
      4▕ use Illuminate\Database\Schema\Blueprint;
      5▕ use Illuminate\Support\Facades\Schema;
      6▕ 
  ➜   7▕ class CreateUserTable extends Migration
      8▕ {
      9▕     /**
     10▕      * Run the migrations.
     11▕      *

  1   C:\xampp\htdocs\chikchika\vendor\filp\whoops\src\Whoops\Run.php:486
      Whoops\Run::handleError("Cannot declare class CreateUserTable, because the name is already in use", "C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php")

  2   [internal]:0
      Whoops\Run::handleShutdown()

C:\xampp\htdocs\chikchika>

This is my "2022_06_21_124328_user.php":

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('username');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->timestamp('account_verified')->nullable();
            $table->integer('level')->default(0); // 0 - member, 1 - moder, 2 - admin
            $table->string('bio')->nullable();
            $table->string('avatar')->nullable();
            $table->string('cover')->nullable();
            $table->integer('audience')->default(0); // 0 - public, 1 - private
            $table->timestamp('birth_date')->nullable();
            $table->string('location')->nullable();
            $table->string('website')->nullable();
            $table->integer('disabled')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

CodePudding user response:

You can use an anonymous migration as Laravel started recommending it as the following:

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
};

Reference: https://laravel.com/docs/9.x/migrations#migration-structure

CodePudding user response:

I hope my solution will help you out: Make your migration name the same as the class name. So, if you have your class name as CreateUserTable the migration name should reflect the exact same thing as:

CreateUserTable => create_user_table.php
  • Related