my data not update in the database in laravel 5.4

时间:2017-05-16 09:39:04

标签: javascript php jquery ajax laravel-5.4

I am using laravel 5.4 in my app I update some record through ajax jquery I get the data from form values through ajax function and append it in array and pass that array (formData in my code) to ajax data then when I request my data in controller it updated as null message in my database.

This is my view jquery function

$('#updateProduct').on('submit', function(e){
    e.preventDefault(e);
    var redirect_url = $(this).find("[name='redirect_url']").val();
    var url = $(this).attr('action');
    var method = $(this).attr('method');
    var video = document.getElementById('videoToUpload').files[0];     
   // console.log(video);
    var formData = new FormData();
    formData.append('_method', 'patch'); 
    formData.append('name', $(this).find("[name='name']").val());
    formData.append('description', $(this).find("[name='description']").val());
    formData.append('brand', $(this).find("[name='brand']").val());
    formData.append('category', $(this).find("[name='category']").val());
    formData.append('condition', $(this).find("[name='condition']").val());
    formData.append('shipper', $(this).find("[name='shipper']").val());
    formData.append('shipping_from', $(this).find("[name='shipping_from']").val());
    formData.append('shipping_paid_by', $(this).find("[name='shipping_paid_by']").val());
    formData.append('shipping_within', $(this).find("[name='shipping_within']").val());
    formData.append('shipping_weight', $(this).find("[name='shipping_weight']").val());
    formData.append('shipping_fee', $(this).find("[name='shipping_fee']").val());
    formData.append('seller_get', $(this).find("[name='seller_get']").val());
    formData.append('price_per_unit', $(this).find("[name='price_per_unit']").val());
    formData.append('selling_fee', $(this).find("[name='selling_fee']").val());
    formData.append('seller_id', $(this).find("[name='seller_id']").val());
    formData.append('is_active', $(this).find("[name='is_active']:checked").val()); 
    console.log(formData);
 $.ajax({
        type: method,
        url: url,
        dataType: 'JSON',
        data: formData,
        contentType: false,
        processData: false,
        success: function(data){
          alert("Products updated successfullly");
          console.log(data);
          //window.location.href = redirect_url;
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.log(JSON.stringify(jqXHR));
          console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
        }
      });

In my controller

 public function update(Request $request, $id)
    {

        return Response::json([
                'message' => $request['name']
        ], 200);
        if(!$request){
            return Response::json([
                'error' => [
                    'message' => 'Kindly provide all the required details'
                ]
            ], 422);
        }
        $product = Product::find($id);
        $product->name = $request['name'];
        $product->sku = $request['sku'];
        $product->slug = $request['slug'];
        $product->description = $request['description'];
        $product->brand = $request['brand'];
        $product->condition = $request['condition'];
        $product->shipper = $request['shipper'];
        $product->shipping_from = $request['shipping_from'];
        $product->shipping_fee = $request['shipping_fee'];
        $product->shipping_paid_by = $request['shipping_paid_by'];
        $product->shipping_within = $request['shipping_within'];
        $product->shipping_weight = $request['shipping_weight'];
        $product->selling_fee = $request['selling_fee'];
        $product->seller_get = $request['seller_get'];
        $product->price_per_unit = $request['price_per_unit'];
        ///$product->seller_id = $request['seller_id'];
        $product->is_active = $request['is_active'];
        $product->save();

        $category = ProductCategory::where('product_id', '=', $id);
        $category->update([
            'category_id' => $request['category']
        ]);

        return Response::json([
                'message' => $product
        ], 200);
    }

0 个答案:

没有答案