首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel 9-不能声明类CreateUsersTable,因为名称已经在使用中了

Laravel 9-不能声明类CreateUsersTable,因为名称已经在使用中了
EN

Stack Overflow用户
提问于 2022-06-21 13:30:22
回答 2查看 1.2K关注 0票数 0

我删除了继Laravel之后的迁移文件,并创建了一个新的迁移文件。血干了,我干了这么久。迁移文件中的类名是CreateUsersTable,我将其更改为CreateUserTable.。我删除了数据库从一开始..。我清理了Laravel的藏身处但是..。字面上什么都没有..。它把它藏在哪里?1-2小时前,我删除了另一个迁移文件,但Laravel并不真正关心它,它在哪里呢?

错误消息:

代码语言:javascript
运行
复制
 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>

这是我的"2022_06_21_124328_user.php":

代码语言:javascript
运行
复制
<?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');
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-21 16:02:56

您可以使用anonymous migration,就像Laravel开始推荐的那样:

代码语言:javascript
运行
复制
<?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');
    }
};

参考资料:https://laravel.com/docs/9.x/migrations#migration-structure

票数 1
EN

Stack Overflow用户

发布于 2022-06-21 16:16:13

我希望我的解决方案将帮助您:使您的迁移名与类名相同。因此,如果类名为CreateUserTable,则迁移名称应反映与以下内容完全相同的内容:

代码语言:javascript
运行
复制
CreateUserTable => create_user_table.php
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72701682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档