/** * @param PropertyOption $options * @return string * @throws \Exception */ public function destroy(PropertyOption $options) { if ($options->id) { if ($options->delete()) { return json_encode(['status' => 'oke']); } } return json_encode(['status' => 'noke']); }
/** * @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]); } } }
/** * @param Product $product * @param PropertyUnit $unit */ protected function prepareProperties(Product $product, PropertyUnit $unit) { $category = $product->mainCategory(); if ($category) { $category->load(['propertyGroups', 'propertyGroups.translations', 'properties', 'properties.translations']); $product->propertyGroups = $category->propertyGroups; $product->propertyProperties = $category->properties->groupBy('group_id'); foreach ($product->propertyGroups as $group) { if (!$product->propertyProperties->has($group->id)) { //make sure we can add to empty groups $product->propertyProperties->put($group->id, new Collection()); } } $product->propertyUnits = $unit->with('translations')->get()->keyBy('id'); $propertyIds = $category->properties->lists('id')->toArray(); if (count($propertyIds)) { $product->propertyOptions = PropertyOption::with(['translations'])->whereIn('property_id', $propertyIds)->get()->groupBy('property_id'); $product->propertyOptions = $product->propertyOptions->map(function ($item) { return $item->keyBy('id'); }); } else { $product->propertyOptions = new Collection(); } $product->hasMainCategory = true; $product->setRelation('properties', $product->properties->keyBy('property_id')); } }