更新表方案而不影响 Laravel 中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20516514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:44:59  来源:igfitidea点击:

Updating table scheme without affecting data in Laravel

phpmysqllaravelmigration

提问by dallardtech

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

我是代码点火器的 Laravel 新手,我喜欢这个框架!我的生活现在轻松多了。

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.

我使用 php artisan 创建了一个带有列的表并输入了一些测试数据。我现在想在不影响当前数据的情况下向数据库添加一些新列,并将新字段设置为空。

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.

我最初的想法是在数据库迁移文件中输入一个新字段并运行“php artisan migrate”,但这只是给了我“没有什么可迁移”的消息,并且确实在我的数据库中输入了新列。

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');
}

}

回答by umefarooq

create new migration with artisan name it addColumnFestivalTable

使用工匠名称创建新的迁移它 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

有关更多信息,请阅读Laravel 5.4 文档