更新表方案而不影响Laravel中的数据 [英] Updating table scheme without affecting data in Laravel

查看:84
本文介绍了更新表方案而不影响Laravel中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel从代码点火器开始的新手,并且我喜欢框架!现在我的生活变得如此轻松.

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

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

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 migration",但这只是给我消息什么都不能迁移",并且确实在数据库中输入了新列.

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

}

推荐答案

使用工匠名称addColumnFestivalTable创建新迁移

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

}

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

for more information read Laravel 5.4 doc

这篇关于更新表方案而不影响Laravel中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆