예제 #1
0
 public function seedProducts($products)
 {
     $categories = Category::all()->lists('id');
     for ($i = 0; $i < $products; $i++) {
         $keys = array_rand($categories, rand(1, 2));
         if (is_int($keys)) {
             $keys = [$keys];
         }
         $sync = [];
         foreach ($keys as $key) {
             $sync[] = $categories[$key];
         }
         $product = Factory::create(new Product(), ['is_enabled' => rand(0, 100) > 20]);
         $size = Factory::make(new Option(), ['name' => 'Size']);
         $size->bindSelections(['id' => [null, null, null], 'name' => ['Small', 'Medium', 'Large']]);
         $size->save();
         $product->options()->add($size);
         $color = Factory::make(new Option(), ['name' => 'Color']);
         $color->bindSelections(['id' => [null, null, null], 'name' => ['Red', 'Green', 'Blue']]);
         $color->save();
         $product->options()->add($color);
         $product->categories()->sync($sync);
         $inventory = Factory::create(new Inventory(), ['product_id' => $product->id, 'quantity' => rand(0, 5)]);
     }
 }