Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
hkguile Avatar asked May 13 '16 02:05

hkguile


People also ask

How do I add a column to an existing migration in Laravel?

The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.

How do I add a column to an existing migration without losing data?

database\migration\add_new_column_to_products_table.php Now you can run migrate command to add this new field. Simply you can run below command to auto add a new field. Hope it can help you. Laravel Migration example tutorial, in this tutorial you have learned how to add new column to a table without losing data.

How do I add a column in Laravel without losing data?

As per the docs, you just need to create a separate migration to create the new column. This will allow you to add a column without resetting or rolling back your tables, and thus prevent you from losing your data.


1 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

like image 61
lagbox Avatar answered Sep 17 '22 14:09

lagbox