I want to know how to add parent_id in products table and parent_product_id in productAttributes table( to filter products with attribute values) 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 productcontroller(products table)
and this ‘parent_product_id’ => $parentProductID, to productAttributeController or ProductAttribute.vue (productAttributes table) and thank you very much
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>