/** * Run the database seeds. * * @return void */ public function run() { DB::statement('SET FOREIGN_KEY_CHECKS=0;'); print_r('truncate' . PHP_EOL); User::truncate(); Category::truncate(); Post::truncate(); Reply::truncate(); $faker = Faker::create(); User::create(['name' => 'Administrator', 'email' => '*****@*****.**', 'avatar' => 'assets/images/batman.png', 'password' => bcrypt('password'), 'remember_token' => str_random(10)]); factory(App\User::class, 50)->create(); $users = User::lists('id')->toArray(); $topics = ['lifestyle', 'nightsale', 'dota2', 'berita', 'televisi']; foreach ($topics as $t) { print_r('Creating ' . $t . PHP_EOL); $category = Category::create(['title' => $t, 'description' => $faker->text, 'slug' => str_slug($t)]); $totalPost = rand(10, 20); print_r(' Creating ' . $totalPost . ' posting' . PHP_EOL); for ($i = 0; $i < $totalPost; $i++) { $timestamp = $faker->dateTimeBetween('-1 year', 'now'); $post = Post::create(['user_id' => $faker->randomElement($users), 'category_id' => $category->id, 'title' => $faker->sentence, 'content' => $faker->paragraph(rand(1, 5)), 'created_at' => $timestamp, 'updated_at' => $timestamp]); if ($faker->boolean(60)) { $totalReply = rand(5, 20); print_r(' Creating ' . $totalReply . ' reply' . PHP_EOL); for ($j = 0; $j < $totalReply; $j++) { $timestamp = $faker->dateTimeBetween('-1 year', 'now'); $reply = Reply::create(['user_id' => $faker->randomElement($users), 'post_id' => $post->id, 'content' => $faker->paragraph(rand(1, 5)), 'created_at' => $timestamp, 'updated_at' => $timestamp]); } $post->count_reply = $totalReply; $post->save(); } } $category->count_posting = $totalPost; $category->save(); print_r('------------------' . PHP_EOL); } DB::statement('SET FOREIGN_KEY_CHECKS=1;'); }