laravel-learn-bbs/database/seeds/TopicsTableSeeder.php

37 lines
1.0 KiB
PHP
Raw Normal View History

2018-01-01 04:00:45 +00:00
<?php
use App\Models\Topic;
use Illuminate\Database\Seeder;
class TopicsTableSeeder extends Seeder
{
public function run()
{
2018-01-01 04:38:52 +00:00
// 所有用户 ID 数组,如:[1,2,3,4]
$user_ids = \App\Models\User::all()->pluck('id')->toArray();
2018-01-01 04:00:45 +00:00
2018-01-01 04:38:52 +00:00
// 所有分类 ID 数组,如:[1,2,3,4]
$category_ids = \App\Models\Category::all()->pluck('id')->toArray();
// 获取 Faker 实例
$faker = app(Faker\Generator::class);
$topics = factory(Topic::class)
->times(100)
->make()
->each(function ($topic, $index)
use ($user_ids, $category_ids, $faker) {
// 从用户 ID 数组中随机取出一个并赋值
$topic->user_id = $faker->randomElement($user_ids);
// 话题分类,同上
$topic->category_id = $faker->randomElement($category_ids);
});
// 将数据集合转换为数组,并插入到数据库中
2018-01-01 04:00:45 +00:00
Topic::insert($topics->toArray());
}
}