Home > Blockchain >  How to move the Laravel timestamps column to a special location
How to move the Laravel timestamps column to a special location

Time:08-09

I want to move timestamps columns to a specific location. Let's pretend I already have an app running on a production server with the following schema: ORDERS

Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

So after a few days, I added the following columns:

  $table->foreignId('user_id')->constrained('users');
            $table->foreignId('address_id')->constrained('user_addresses');
            $table->string('payment_method')->nullable();
            $table->string('payment_status')->default('pending');
            $table->string('order_status')->default('in_cart');
            $table->text('note')->nullable();
            $table->decimal('total', 8, 2)->default(0);
            $table->string('waybill')->nullable();
            $table->string('logistic')->nullable();
            $table->dateTime('payment_at')->nullable();

The problem here is that all these columns are added after timestamps, but I don't want to add them after timestamps. I want to add them between id and timestamps. Also, I don't want to lose already created timestamps.

CodePudding user response:

You can use the after() column modifier to add columns after (obviously) another column. However, as of Laravel 8.27 you can group columns making life a little simpler:

Schema::table('orders', function ($table) {
    $table->after('name', function ($table) {
        $table->foreignId('user_id')->constrained('users');
        $table->foreignId('address_id')->constrained('user_addresses');
        $table->string('payment_method')->nullable();
        $table->string('payment_status')->default('pending');
        $table->string('order_status')->default('in_cart');
        $table->text('note')->nullable();
        $table->decimal('total', 8, 2)->default(0);
        $table->string('waybill')->nullable();
        $table->string('logistic')->nullable();
        $table->dateTime('payment_at')->nullable();
    });
});

That should place all columns defined inside the after method after the name column but before the timestamps columns.

CodePudding user response:

You can follow custom order for solve this issue

$table->after('name', function ($table) {
      $table->foreignId('user_id')->constrained('users');
      $table->foreignId('address_id')->constrained('user_addresses');
      $table->string('payment_method')->nullable();
      $table->string('payment_status')->default('pending');
      $table->string('order_status')->default('in_cart');
      $table->text('note')->nullable();
      $table->decimal('total', 8, 2)->default(0);
      $table->string('waybill')->nullable();
      $table->string('logistic')->nullable();
      $table->dateTime('payment_at')->nullable();
});

Laravel have ->after('column') column modifier. Place the column "after" another column (MySQL). Hope it's work for you

  • Related