hkguile hkguile - 6 months ago 101
PHP Question

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

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?

Answer

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

Comments