database/seeds/ArticleTableSeeder.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();
        $category_ids = Category::all()->pluck('id')->toArray();
        $images = [
            'https://lccdn.phphub.org/uploads/images/201802/28/1/Jk8mC7SGI5.jpg?imageView2/1/w/600/h/296',
            'https://lccdn.phphub.org/uploads/images/201803/08/1/mIpeaUvQ7Y.jpeg?imageView2/1/w/600/h/296',
            'https://lccdn.phphub.org/uploads/images/201801/24/1/rgMAkwm5JX.jpeg?imageView2/1/w/600/h/296',
            'https://lccdn.phphub.org/uploads/images/201806/21/1/gH3fzveyW3.jpg?imageView2/1/w/600/h/296',
            'https://lccdn.phphub.org/uploads/images/201803/19/1/lDtmqCGQJJ.jpeg?imageView2/1/w/600/h/296',
            'https://lccdn.phphub.org/uploads/images/201803/13/1/DCYvMliGXu.jpg?imageView2/1/w/600/h/296',
        ];
        $faker = app(Faker\Generator::class);
        $articles = factory(Article::class)
        				->times(100)
        				->make()
        				->each(function ($article, $index)
        					use ($user_ids, $category_ids, $images, $faker)
        {
            $article->user_id = $faker->randomElement($user_ids);
            $article->category_id = $faker->randomElement($category_ids);
            $article->image = $faker->randomElement($images);
        });
        Article::insert($articles->toArray());
    }
public function run ( ) { $ user_ids = User :: all ( ) -> pluck ( 'id' ) -> toArray ( ) ; $ category_ids = Category :: all ( ) -> pluck ( 'id' ) -> toArray ( ) ; $ images = [ 'https://lccdn.phphub.org/uploads/images/201802/28/1/Jk8mC7SGI5.jpg?imageView2/1/w/600/h/296' , 'https://lccdn.phphub.org/uploads/images/201803/08/1/mIpeaUvQ7Y.jpeg?imageView2/1/w/600/h/296' , 'https://lccdn.phphub.org/uploads/images/201801/24/1/rgMAkwm5JX.jpeg?imageView2/1/w/600/h/296' , 'https://lccdn.phphub.org/uploads/images/201806/21/1/gH3fzveyW3.jpg?imageView2/1/w/600/h/296' , 'https://lccdn.phphub.org/uploads/images/201803/19/1/lDtmqCGQJJ.jpeg?imageView2/1/w/600/h/296' , 'https://lccdn.phphub.org/uploads/images/201803/13/1/DCYvMliGXu.jpg?imageView2/1/w/600/h/296' , ] ; $ faker = app ( Faker \ Generator :: class ) ; $ articles = factory ( Article :: class ) -> times ( 100 ) -> make ( ) -> each ( function ( $ article , $ index ) use ( $ user_ids , $ category_ids , $ images , $ faker ) { $ article -> user_id = $ faker -> randomElement ( $ user_ids ) ; $ article -> category_id = $ faker -> randomElement ( $ category_ids ) ; $ article -> image = $ faker -> randomElement ( $ images ) ; } ) ; Article :: insert ( $ articles -> toArray ( ) ) ; }
Run the database seeds.
Run the database seeds .
// 所有用户 ID， 如：[1,2,3,4] // 所有分类 ID， 如：[1,2,3,4] // 文章封面图片 // 获取 faker 实例 // 从用户 ID 数组中随机取出一个并赋值 // 从分类 ID 数组中随机取出一个并赋值 // 从图片数组总随机取出一个并赋值