/** * Run the database seeds. * * @return void */ public function run() { /* $shuffle = function($tags, $num) { $s=[]; while($num>0) { $s[] = $tags[$num]; $num--; } return $s; }; $max = $this->tag->count(); $tags = $this->tag->lists('id'); factory(App\Product::class, 15)->create()->each(function ($product) use ($max, $tags, $shuffle) { $product->tags()->attach($shuffle($tags, rand(1, $max - 1))); }); */ factory(App\Product::class, 15)->create()->each(function ($product) { $nrb_insert = rand(1, 4); $tab_result = array(); $nbr_tags = App\Tag::count(); $tab_result = array(); while ($nrb_insert != 0) { $candidat = rand(1, $nbr_tags); $test = array_search($candidat, $tab_result); if (!is_int($test)) { array_push($tab_result, $candidat); } $nrb_insert--; } foreach ($tab_result as $value) { $product->tags()->attach($value); } }); }