I want to know how to add the value of parent_id in product table and value of parent_product_id in productAttributes table (to filter products with attribute values) I have parent_id in product table and parent_product_id in productAttribute table but I want to add the value of them in database when I add attribute to product in product/edit page in admin section
so please how to do that if I have this code with laravel and vuejs :
I want to add this ‘parent_id’ => $product->id, to the productcontroller (product table)
and this ‘parent_product_id’ => $parentProductID, to productAttributeController or ProductAttribute.vue(productAttributes table) and thank you very much
I want to put parent_id and parent_product_id like this code in the other code:
/**
* Generate product variants for the configurable product
*
* @param Product $product product object
* @param array $params params
*
* @return void
*/
private function _generateProductVariants($product, $params)
{
$configurableAttributes = $this->_getConfigurableAttributes();
$variantAttributes = [];
foreach ($configurableAttributes as $attribute) {
$variantAttributes[$attribute->code] = $params[$attribute->code];
}
$variants = $this->_generateAttributeCombinations($variantAttributes);
if ($variants) {
foreach ($variants as $variant) {
$variantParams = [
'parent_id' => $product->id,
'user_id' => Auth::user()->id,
'sku' => $product->sku . '-' .implode('-', array_values($variant)),
'type' => 'simple',
'name' => $product->name . $this->_convertVariantAsName($variant),
];
$variantParams['slug'] = Str::slug($variantParams['name']);
$newProductVariant = Product::create($variantParams);
$categoryIds = !empty($params['category_ids']) ? $params['category_ids'] : [];
$newProductVariant->categories()->sync($categoryIds);
$this->_saveProductAttributeValues($newProductVariant, $variant, $product->id);
}
}
}
/**
* Save the product attribute values
*
* @param Product $product product object
* @param array $variant variant
* @param int $parentProductID parent product ID
*
* @return void
*/
private function _saveProductAttributeValues($product, $variant, $parentProductID)
{
foreach (array_values($variant) as $attributeOptionID) {
$attributeOption = AttributeOption::find($attributeOptionID);
$attributeValueParams = [
'parent_product_id' => $parentProductID,
'product_id' => $product->id,
'attribute_id' => $attributeOption->attribute_id,
'text_value' => $attributeOption->name,
];
ProductAttributeValue::create($attributeValueParams);
}
}
but I have a different code so I want to know how to add the value of parent_id and parent_product_id with this code:
ProductController:
public function store(StoreProductFormRequest $request)
{
$params = $request->except('_token');
$product = $this->productRepository->createProduct($params);
if (!$product) {
return $this->responseRedirectBack('Error occurred while creating product.', 'error', true, true);
}
return $this->responseRedirect('admin.products.index', 'Product added successfully' ,'success',false, false);
}
productRepository:
public function createProduct(array $params)
{
try {
$collection = collect($params);
$featured = $collection->has('featured') ? 1 : 0;
$status = $collection->has('status') ? 1 : 0;
$merge = $collection->merge(compact('status', 'featured'));
$product = new Product($merge->all());
$product->save();
if ($collection->has('categories')) {
$product->categories()->sync($params['categories']);
}
ProductInventory::updateOrCreate(['product_id' => $product->id], ['qty' => $params['qty']]);
return $product;
} catch (QueryException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
}
productAttributeController:
public function productAttributes(Request $request)
{
$product = Product::findOrFail($request->id);
return response()->json($product->attributes);
}
public function addAttribute(Request $request)
{
$productAttribute = ProductAttribute::create($request->data);
if ($productAttribute) {
return response()->json(['message' => 'Product attribute added successfully.']);
} else {
return response()->json(['message' => 'Something went wrong while submitting product attribute.']);
}
}
productAttribute.vue:
<script>
export default {
name: "product-attributes",
props: ['productid'],
data() {
return {
productAttributes: [],
attributes: [],
attribute: {},
attributeSelected: false,
attributeValues: [],
value: {},
valueSelected: false,
currentAttributeId: '',
currentValue: '',
currentQty: '',
currentPrice: '',
}
},
created: function() {
this.loadAttributes();
this.loadProductAttributes(this.productid);
},
methods: {
selectAttribute(attribute) {
let _this = this;
this.currentAttributeId = attribute.id;
axios.post('/admin/products/attributes/values', {
id: attribute.id
}).then (function(response){
_this.attributeValues = response.data;
}).catch(function (error) {
console.log(error);
});
this.attributeSelected = true;
},
selectValue(value) {
this.valueSelected = true;
this.currentValue = value.value;
this.currentQty = value.quantity;
this.currentPrice = value.price;
},
addProductAttribute() {
if (this.currentQty === null || this.currentPrice === null) {
this.$swal("Error, Some values are missing.", {
icon: "error",
});
} else {
let _this = this;
let data = {
attribute_id: this.currentAttributeId,
value: this.currentValue,
quantity: this.currentQty,
price: this.currentPrice,
product_id: this.productid,
};
axios.post('/admin/products/attributes/add', {
data: data
}).then (function(response){
_this.$swal("Success! " + response.data.message, {
icon: "success",
});
_this.currentValue = '';
_this.currentQty = '';
_this.currentPrice = '';
_this.valueSelected = false;
}).catch(function (error) {
console.log(error);
});
this.loadProductAttributes(this.productid);
}
},
}
}
</script>
thank yo very much