Home » Php » php – laravel 5.2 migration add column/field before or after specific column

php – laravel 5.2 migration add column/field before or after specific column

Posted by: admin July 12, 2020 Leave a comment

Questions:

i know how to add column/field to to database for example

<?php

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

class AddStatusToPhoto extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('photos', function(Blueprint $table)
        {
            $table->integer('status');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }


}

but it will append to the last field of the table, but i want to add to specific column e.g insert the field after created_at, is it possible to do that?

How to&Answers:

There is a after modifier you can use if you are on MySQL.

$table->integer('status')->after('created_at');

->after('column') Place the column “after” another column (MySQL Only)

Laravel Docs – Migrations – Creating Columns – Column Modifiers