Php Laravel 5.1为一个新列播种

Php Laravel 5.1为一个新列播种,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个名为locations的数据库表,需要添加一个新的status列。我已经设置了以下迁移文件,当我运行php artisan migrate时,该文件会添加fine字段,但是如何为表中的每一行插入Active <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddStatusToLocationsTable extends Mig

我有一个名为
locations
的数据库表,需要添加一个新的
status
列。我已经设置了以下迁移文件,当我运行
php artisan migrate
时,该文件会添加fine字段,但是如何为表中的每一行插入
Active

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStatusToLocationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('locations', function(Blueprint $table)
        {
            $table->string('status')->after('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('locations', function(Blueprint $table)
        {
            $table->dropColumn('status');
        });
    }
}
您需要使用。您可以为locations表创建模型,也可以使用插入的
DB::table
方式

然后,您可以运行php artisan migrate--seed来创建模式并填充数据库。

您有两种解决方案

您完全可以为该表的现有播种机添加一个值

您还可以创建一个新的播种器,在其中获取每个位置,添加新的字段值,然后像这样保存它

foreach(Location::all() as $location){
    $location->status = 'Active';
    $location->save();
}

您可以将字段设置为默认值

$table->string('status')->after('email')->default('active')
如果只需要更新当前记录,可以进行种子设定:

运行此artisan命令:

php artisan make:seeder AddStatusSeeder    
然后在AddStatusSeeder中:

<?php

use Illuminate\Database\Seeder;

class AddStatusSeeder extends Seeder
{
     /**
     * Run the database seeds.
     *
     * @return void
     */
     public function run()
     {
         DB::table('locations')->update([
            'status' => 'Active'
         ]);
     }
 }

是的,我已经有了位置表的模型和播种机文件。但我的问题是,我怎样才能创建一个新的专栏呢?谢谢上面的内容。当你想要一个唯一的ID时,有没有关于如何修改上述内容的想法?当前,当我生成UUID时,相同的ID用相同的值填充每一行。代码:'public_id'=>(string)Uuid::generate(4)DB::table('your-table')->update([…])不带,其中条件意味着它将更新所有记录。您需要遍历所有行并单独更新UUID。