コード例 #1
0
 public function run()
 {
     $users = MyUser::all();
     $ids = $users->lists('id');
     $faker = Factory::create();
     foreach (MyPost::all() as $post) {
         if (!empty($ids)) {
             $postuser = new MyPostUser();
             $postuser->user_id = $ids[$faker->numberBetween(0, count($ids) - 1)];
             $postuser->post_id = $post->id;
             $postuser->save();
         }
     }
 }
コード例 #2
0
 public function run()
 {
     $tags = MyTag::all();
     $ids = $tags->lists('id');
     $total = count($ids);
     $faker = Factory::create();
     foreach (MyPost::all() as $post) {
         if (!empty($ids)) {
             // Generate random number of tags
             foreach (range(1, $faker->numberBetween(0, $total - 1)) as $value) {
                 $postTag = new MyPostTag();
                 $postTag->tag_id = $ids[$faker->numberBetween(0, $total - 1)];
                 $postTag->post_id = $post->id;
                 try {
                     $postTag->save();
                 } catch (\Exception $e) {
                     // Skip
                 }
             }
         }
     }
 }