/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); $limit = 20; for ($i = 0; $i < $limit; $i++) { DB::table('tags')->insert(['value' => $faker->word]); } $stories = Story::get(); foreach ($stories as $story) { $numbers = range(1, 20); shuffle($numbers); $story->tags()->attach($numbers[0]); $story->tags()->attach($numbers[2]); $story->tags()->attach($numbers[4]); } }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); $limit = 20; for ($i = 0; $i < $limit; $i++) { DB::table('tags')->insert(['value' => $faker->word, 'created_at' => $faker->dateTime(), 'updated_at' => $faker->dateTime()]); } $stories = Story::get(); foreach ($stories as $story) { $rand = rand(0, 20); DB::table('story_tag')->insert(['story_id' => $story->id, 'tag_id' => $rand, 'created_at' => $faker->dateTime(), 'updated_at' => $faker->dateTime()]); $rand = rand(0, 20); DB::table('story_tag')->insert(['story_id' => $story->id, 'tag_id' => $rand, 'created_at' => $faker->dateTime(), 'updated_at' => $faker->dateTime()]); $rand = rand(0, 20); DB::table('story_tag')->insert(['story_id' => $story->id, 'tag_id' => $rand, 'created_at' => $faker->dateTime(), 'updated_at' => $faker->dateTime()]); } }
public function run() { $faker = Faker\Factory::create(); $limit = 20; for ($i = 0; $i < $limit; $i++) { DB::table('tags')->insert(['value' => $faker->word]); } $stories = App\Story::get(); $array = array(); for ($i = 0; $i < $limit; $i++) { $array[$i] = $i + 1; } foreach ($stories as $story) { # code... $story->tags()->attach($faker->randomElements($array, $count = 3)); } }
/** * Run the database seeds. * * @return void */ public function run() { // DB::table('story_tag')->delete(); DB::table('tags')->delete(); $tagLove = Tag::create(array('id' => 1, 'value' => 'Love')); $tagComedy = Tag::create(array('id' => 2, 'value' => 'comedy')); $tagCrime = Tag::create(array('id' => 3, 'value' => 'crime')); $tagThriller = Tag::create(array('id' => 4, 'value' => 'thriller')); $this->command->info('seeding finished for tags and story_tag!'); $stories = App\Story::get(); foreach ($stories as $story) { $this->command->info($story->id); $tag_ids = array(1, 2, 3, 4); $story->tags()->attach($tag_ids); } $this->command->info('seeding finished for tags and story_tag!'); }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker::create(); $stories = Story::get()->lists('id')->all(); $tags = Tag::get()->lists('id')->all(); foreach (range(1, 10) as $index) { DB::table('story_tag')->insert(['story_id' => $faker->randomElement($stories), 'tag_id' => $faker->randomElement($tags)]); } }