dimanche 5 janvier 2020

Cannot save value using ajax in laravel

I'm using laravel and trying to save data using post through ajax but data is not saved in database. I'm getting following error: jquery.min.js:2 POST http://localhost:8000/admin/products/attributes/add 500 (Internal Server Error). My code is as follows:

view:

<script>
     $("#add_attributes_info").click(function(e){
        e.preventDefault(); 
        $.ajax({
            type: "POST",
            url: '/admin/products/attributes/add',
            data: $('#frmattributes').serialize(),
            success: function(msg) {
                console.log('success'+msg);
              }
        });
        });
</script>
 <form action="#" id="frmattributes" method="POST">
            <h3 class="tile-title">Add Attributes To Product</h3>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="values">Select an value <span class="m-l-5 text-danger"> *</span></label>
                        <select id="attribute_values" name="value" class="form-control custom-select mt-15">

                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label class="control-label" for="quantity">Quantity</label>
                        <input class="form-control" name="quantity" type="number" id="quantity"/>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label class="control-label" for="price">Price</label>
                        <input class="form-control" name="price" type="text" id="price"/>
                        <small class="text-danger">This price will be added to the main price of product on frontend.</small>
                    </div>
                </div>
                <div class="col-md-12">
                    <button class="btn btn-sm btn-primary" id="add_attributes_info">
                        <i class="fa fa-plus"></i> Add
                    </button>
                </div>
            </div>
            </form>

Controller:

  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.']);
        }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire