Updating table scheme without affecting data in Laravel(更新表方案而不影响 Laravel 中的数据)
问题描述
我是来自 code igniter 的 Laravel 新手,我喜欢这个框架!我现在的生活轻松多了.
I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.
我使用 php artisan 创建了一个包含列的表并输入了一些测试数据.我现在想在不影响当前数据的情况下向数据库添加一些新列,并将新字段设置为空.
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 migrate",但这只是给了我没有要迁移"的消息,并且确实在我的数据库中输入了新列.
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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
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 文档
这篇关于更新表方案而不影响 Laravel 中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更新表方案而不影响 Laravel 中的数据


基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01