简体   繁体   中英

Updating table scheme without affecting data in Laravel

I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.

I have created a table with columns using php artisan and entered some test data. I now want to add a few new columns to the database without affecting the current data, and setting the new fields to be null.

My inital thought was to enter a new field in the database migrate file and the run "php artisan migrate", but this just gave me the message "nothing to migrate" and did enter the new column in my database.

Here is my database migrate file:

<?php

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

class CreateFestivalsTable extends Migration {

public function up()
{
    Schema::create('festivals', function(Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('title');
        $table->timestamps();
    });

}

public function down()
{
    Schema::drop('festivals');
}

}

create new migration with artisan name it addColumnFestivalTable

<?php

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

class addColumnFestivalTable extends Migration {

public function up()
{
    Schema::table('festivals', function($table)
    {
        $table->string('new_col_name');
     });

}

public function down()
{
    Schema::table('festivals', function($table)
    {
       $table->dropColumn('new_col_name');
    });
}

}

for more information read Laravel 5.4 doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM