php 在 Laravel 5 迁移中更新表并添加数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37478526/
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-08-26 01:15:20  来源:igfitidea点击:

Update table and add data in a Laravel 5 Migration

phppostgresqllaravel

提问by Deric Lima

I need to add a new column in my laravel Project, no problem for this, I used the Schema::table()to update and it's ok. Now I need to find out how many records I have on this table and update with some value.

我需要在我的 Laravel 项目中添加一个新列,这没问题,我使用了Schema::table()更新,没关系。现在我需要找出我在这个表上有多少记录并更新一些值。

I have the table Warrants:

我有桌子Warrants

Schema::create('warrant_grants', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('warrant_plan_id');
    $table->integer('shareholder_id');
});

So I created the new field with a new migration file:

所以我用一个新的迁移文件创建了新字段:

Schema::table('warrant_grants',function ($table) {
    $table->string('name',100);
});

Now I need to update this field namein the table with some values, for example if the table has 100 records, then I need to insert in every row the value "Warrant-X" where X is a number starting with 1 to 100. For example:

现在我需要name用一些值更新表中的这个字段,例如,如果表有 100 条记录,那么我需要在每一行中插入值“Warrant-X”,其中 X 是一个从 1 到 100 的数字。对于例子:

Warrant-1, Warrant-2, ....Warrant-100.

权证 1、权证 2、....权证 100。

I spent hours looking for some way to do this using Seeds but I didn't found. So basically i have two questions:

我花了几个小时寻找使用种子的方法,但我没有找到。所以基本上我有两个问题:

  • Can I use Seeds in Laravel 5 to update values or I can just insert them?
  • Can I create some SQL inside the Seeds (or migrations) to do this update for me?
  • 我可以在 Laravel 5 中使用种子来更新值还是我可以插入它们?
  • 我可以在种子(或迁移)中创建一些 SQL 来为我执行此更新吗?

采纳答案by Deric Lima

Based on this link i found the answer: https://stackoverflow.com/a/23506744/4650792

基于此链接,我找到了答案:https: //stackoverflow.com/a/23506744/4650792

Schema::table('warrant_grants',function ($table){
        $table->string('name',100)->after('id')->nullable();
    });

    $results = DB::table('warrant_grants')->select('id','name')->get();

    $i = 1;
    foreach ($results as $result){
        DB::table('warrant_grants')
            ->where('id',$result->id)
            ->update([
                "name" => "Warrant-".$i
        ]);
        $i++;
    }

Thanks for the help anyway guys.

无论如何,感谢您的帮助。

回答by Oliver Maksimovic

Yes, you can perform updates/inserts/whatever in your migrations. For example:

是的,您可以在迁移中执行更新/插入/任何操作。例如:

Schema::table('warrant_grants', function($table) {
    $table->string('name', 100);
});

$i = 1;
foreach (WarrantGrants::all() as $warrant_grant) {
    $warrant_grant->update([
      'name' => 'Warrant-' . $i
    ]);

    $i++;
}

回答by vfsoraki

Other answers are correct. But note that if you have a lot of records, updating all of them with ORM can take time. Use raw SQL queries to do that faster.

其他答案都是正确的。但请注意,如果您有很多记录,则使用 ORM 更新所有记录可能需要时间。使用原始 SQL 查询可以更快地完成这项工作。

Schema::table('warrant_grants',function ($table){
        $table->string('name',100)->after('id')->nullable();
    });
DB::raw("UPDATE warrant_grants SET name=name+id");

The SQL query is not exact, and you have to make it for your own DB, but you get the point.

SQL 查询并不准确,您必须为自己的数据库制作它,但您明白了。

回答by rh16

Another possible syntax to achieve this:

实现此目的的另一种可能的语法:

DB::table('warrant_grants')
   ->where('id',$result->id)
   ->update([
      "name" => DB::raw("'Warrant-' + `name`")
   ]);

This allows the update to be done as one batch rather than iterating over results, and retains most of the familiar Eloquent syntax rather than falling back to just using raw SQL.

这允许更新作为一批完成而不是迭代结果,并保留大部分熟悉的 Eloquent 语法而不是只使用原始 SQL。

The string concatenation syntax may need to be changed depending on the SQL variant used.

根据所使用的 SQL 变体,可能需要更改字符串连接语法。