I added parent_product_id to productAttributes table and parent_product_id has the same value of product_id and the problem is that I can’t store the value of parent_product_id so please how to do that and thank you very much .(laravel and vuejs)
and I tried that in productAttribute.vue:
props: ['productid'],
data() {
return {
parent_product_id: '',
currentAttributeId: '',
currentValue: '',
currentQty: '',
currentPrice: '',
}
},
addProductAttribute() {
if
...
else {
let _this = this;
let data = {
parent_product_id: this.ParentProductId,
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
})
so how to save this value
product.php:
public function attributes()
{
return $this->hasMany(ProductAttribute::class, 'parent_product_id');
}
productAttribute.php:
protected $fillable = ['parent_product_id','attribute_id', 'product_id', 'value', 'quantity', 'price'];
public function product()
{
return $this->belongsTo(Product::class);
}
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>