为特定的值播种

时间:2019-01-24 19:29:00

标签: laravel laravel-seeding

我在Laravel中使用播种机。我有一个名为RewriteEngine on RewriteCond %{REMOTE_USER}%{REQUEST_URI} !^([^/]+/)\1 RewriteCond %{HTTP_HOST} ^([^\.]+).dev.company.com$ RewriteRule ^(.+)$ /%{REMOTE_USER}/%1/html/$1 的表,该表有两列:nationalitiesid我想播多个具有特定唯一值(德语,法语等)的记录。

NationalitySeeder.php

name.

DatabaseSeeder.php

use Illuminate\Database\Seeder;
use App\Nationality;

class NationalitySeeder extends Seeder
{
    public function run()
    {
        $nation = Nationality::create([
            'name' => 'american'
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

database/factories中创建一个名为NationalityFactory.php的工厂文件 包含以下代码:

use Faker\Generator as Faker;

$factory->define(App\Nationality::class, function (Faker $faker) {
    return [
        'name' => $faker->languageCode,
    ];
});

在NationalitySeeder.php中,输入以下代码

$nationalities = ['German', 'French', 'etc.'];

factory(App\Nationality::class, count($nationalities))
           ->create()
           ->each(function ($nationality, $key) use ($nationalities) {
                $nationality->name = $nationalities[$key];
            });

这将根据您使用Laravel工厂在阵列中配置的数量来确定国籍。