protected function properties(Product $product, Category $category) { $properties = Property::where('category_id', $category->id)->get(); $properties = $properties->random(rand(5, 10)); $values = new Collection(); foreach ($properties as $property) { $payload = ['property_id' => $property->id]; if ($property->options->count()) { $payload['option_id'] = $property->options->random(1)->id; } $values->push(factory(PropertyValue::class)->make($payload)); } $product->properties()->saveMany($values); }