How to store data?

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>

Hi,

Try this

debug first if you hit that action, and see if there are values on data request ?


 public function addAttribute(Request $request){

  dd($request->data->all())

  ... 
  ....
 ...
 }

ProductAttribute::create($request->data->all());

@jemz thank you very very much
I did what you suggest but when I click save button nothing happen(no success message) and I get error in devtools in network: in Headers:POST http://localhost/admin/products/attributes/add 500 (Internal Server Error) app.js:267 and in Payload :{data: {attribute_id: 4, value: “df”, quantity: “1”, price: “2.00”, product_id: 45}}

Hi, is your problem solve now ?

did you also tried ?

dd($request->all())

Check in network response tab if you see the data.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.