V
V
Vova1357982021-01-02 12:42:04
Laravel
Vova135798, 2021-01-02 12:42:04

Why does it give an error when creating a model with migration?

When creating a model and migration, it gave an error:

Model created successfully.

   InvalidArgumentException 

  A CreateUsersTable class already exists.

  at vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.php:102
     98▕             }
     99▕         }
    100▕ 
    101▕         if (class_exists($className = $this->getClassName($name))) {
  ➜ 102▕             throw new InvalidArgumentException("A {$className} class already exists.");
    103▕         }
    104▕     }
    105▕ 
    106▕     /**

      +28 vendor frames 
  29  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

I understand that the error occurred while connecting to the database. I am using mysql on mamp with port 8888. The port of the database itself is 8889

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '8889'),
            'database' => env('DB_DATABASE', 'laravel'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'root'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2021-01-02
@delphinpro

Update Laravel to version 8.74 and switch to anonymous migration classes.
To do this, create a directory /stubs
Inside create three files

migration.create.stub

<?php

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

return new class extends Migration {
  public function up()
  {
    Schema::create('{{ table }}', function (Blueprint $table) {
      $table->id();
      $table->timestamps();
    });
  }

  public function down()
  {
    Schema::dropIfExists('{{ table }}');
  }
};


migration.stub
<?php

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

return new class extends Migration {
  public function up()
  {
  }

  public function down()
  {
  }
};

migration.update.stub
<?php

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

return new class extends Migration {
  public function up()
  {
    Schema::table('{{ table }}', function (Blueprint $table) {
    });
  }

  public function down()
  {
    Schema::table('{{ table }}', function (Blueprint $table) {
    });
  }
};

Now you can name the migrations whatever you like, there will be no name conflicts.
PS. In nine, anonymous migration classes will still go by default.

L
Loli E1ON, 2021-01-02
@E1ON

A CreateUsersTable class already exists.
Translation magic:
Класс CreateUsersTable уже существует.
You have successfully created an entity, but a migration with the same name already exists.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question