/** * Move from one group to another. * @param Request $request * @param PropertyGroup $groups * @param Property $properties */ public function moveProperty(Request $request, PropertyGroup $groups, Property $properties) { $from = $groups->find($request->get('from')); $to = $groups->find($request->get('to')); $property = $properties->find($request->get('property')); $position = $request->get('position'); //we move away from a group, meaning, everything that was behind this element //will now get a lower sort. Property::where('category_id', $property->category_id)->where('group_id', $from->id)->where('sort', '>', $property->sort)->update(['sort' => \DB::raw('sort - 1')]); //items that need to come behind our property get a higher sort Property::where('category_id', $property->category_id)->where('group_id', $to->id)->where('sort', '>=', $position)->update(['sort' => \DB::raw('sort + 1')]); //finally we save the property with it's new group and position $property->group_id = $to->id; $property->sort = $position; $property->save(); }
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); }
/** * @param $product * @param $data * @param $category * @param $group */ protected function productProperties($product, $data, $category, $group) { foreach ($data->specs as $spec) { if (!empty($spec->spec) && !empty($spec->value)) { $prop = PropertyTranslation::where('name', $spec->spec)->first(); if (!$prop) { $prop = Property::create(['type' => 'options', 'category_id' => $category->id, 'group_id' => $group->id, 'nl' => ['name' => $spec->spec], 'en' => ['name' => $spec->spec]]); $prop = $prop->id; } else { $prop = $prop->property_id; } $option = PropertyOptionTranslation::join('product_properties_options', 'product_properties_options.id', '=', 'product_properties_options_translations.option_id')->where('property_id', $prop)->where('name', $spec->value)->first(); if (!$option) { $option = PropertyOption::create(['property_id' => $prop, 'nl' => ['name' => $spec->value], 'en' => ['name' => $spec->value]]); $option = $option->id; } else { $option = $option->option_id; } $value = PropertyValue::create(['product_id' => $product->id, 'property_id' => $prop, 'option_id' => $option]); } } }