database/seeds/ReplyTableSeeder.php
/**
     * Run the database seeds.
     *
     * @return void
     */
Run the database seeds.
@return void
Run the database seeds . @return void
public function run()
    {
        $user_ids = User::all()->pluck('id')->toArray();
        $article_ids = Article::all()->pluck('id')->toArray();
        $faker = app(Faker\Generator::class);
        $replys = factory(Reply::class)
                        ->times(1000)
                        ->make()->each(function ($reply, $index)
                        use ($user_ids, $article_ids, $faker)
        {
            $reply->user_id = $faker->randomElement($user_ids);
            $reply->article_id = $faker->randomElement($article_ids);
        });
        Reply::insert($replys->toArray());
    }
public function run ( ) { $ user_ids = User :: all ( ) -> pluck ( 'id' ) -> toArray ( ) ; $ article_ids = Article :: all ( ) -> pluck ( 'id' ) -> toArray ( ) ; $ faker = app ( Faker \ Generator :: class ) ; $ replys = factory ( Reply :: class ) -> times ( 1000 ) -> make ( ) -> each ( function ( $ reply , $ index ) use ( $ user_ids , $ article_ids , $ faker ) { $ reply -> user_id = $ faker -> randomElement ( $ user_ids ) ; $ reply -> article_id = $ faker -> randomElement ( $ article_ids ) ; } ) ; Reply :: insert ( $ replys -> toArray ( ) ) ; }
Run the database seeds.
Run the database seeds .
// 所有用于 ID 数组 // 所有的话题 ID 数组 // 获取 Faker 实例 // 从用户 ID 数组中随机取出一个饼赋值 // 从话题 ID 数组中随机取出一个饼赋值 // 将数据集合转换为数组，并插入到数据库中