laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.64k stars 11.04k forks source link

Making migrations. Moved the table name to a constant. Added a check for the existing of a table #53542

Closed NikitinUser closed 4 days ago

NikitinUser commented 5 days ago

When creating a migration via make:migration, the table name is hardcoded in several places. Also, there is no check in the "up" method that the table has not been created yet (which is strange, because in "down" the presence of the table is checked). As a result, when creating a new migration, you have to eliminate these points yourself every time. I really want these things to be out of the box.

Now:

public function up(): void
{
    Schema::create('mytable', function (Blueprint $table) {
    ...

public function down(): void
{
    Schema::dropIfExists('mytable');
    ...

My way:

public const TABLENAME = 'mytable';

public function up(): void
{
    if (Schema::hasTable(self::TABLENAME)) {
        return;
    }

    Schema::create(self::TABLENAME, function (Blueprint $table) {
    ...
}

public function down(): void
{
    Schema::dropIfExists(self::TABLENAME);

Benefit - is subjectively convenient This does not break any existing features as it does not affect logic. These are minor edits in migration templates. The tests have been updated according to the new templates