【问题标题】:Is there a way to keep the data when updating a database table using Laravel 5?使用 Laravel 5 更新数据库表时,有没有办法保留数据?
【发布时间】:2016-09-20 15:15:23
【问题描述】:

我有一个可以运行的 Laravel 应用程序,我需要更新一个模型并向数据库添加一个新列。为此,我在 database/migrations/table.php 文件中添加了以下代码:

Schema::table('client', function (Blueprint $table) {
        $table->string('newColumn');
});

当我尝试更新数据库(使用php artisan migrate)时,我收到消息“Nothing to migrate”。我注意到应用此更改的唯一方法是执行php artisan migrate:refresh 或重置,但这两个命令都会删除数据库中的每一行。

是否有在不删除所有内容的情况下更新我的数据库列?

【问题讨论】:

  • 你有迁移表吗?您是否使用 php artisan make:migration add_newColumn_to_client 创建了迁移?
  • 不,我使用 make 命令创建了数据库迁移表,但没有添加列,所以这就是问题所在。我在迁移文件中添加了新字段。谢谢!
  • 好吧,很高兴就这么简单!

标签: laravel laravel-5 laravel-migrations


【解决方案1】:

The Maniac 描述的完整解决方案是:

  • 使用:php artisan make:migration add_newColumn_to_client 创建新列。
  • 将以下代码添加到新创建的文件(date_add_newColumn_to_client)中:

    public function up()
    {
        Schema::table('client', function (Blueprint $table) {
            $table->string('newColumn');
        });
    }
    
  • 运行php artisan migrate

【讨论】:

    【解决方案2】:

    如果您只添加一些列[s],您可以使用完整的插入语句导出数据,运行 migrate:refresh 并重新导入数据。对于 MySQL:

    mysqldump -u {USER} -p {DATABASE} --no-create-db --no-create-info --complete-insert > yourdata.sql
    artisan migrate:refresh
    mysql -u {USER} -p {DATABASE} < yourdata.sql
    

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多