lundi 20 juin 2016

How to extract data in laravel controller from object array in HTTP request passed via ajax

i am developing a POS system in laravel 5.2 .From my sales page i am sending an array which contains 1 bill-info object and 1 product-list object array.My controller is receiving the data correctly,but i cant figure out how to extract them in my controller.I have gone through laravel documentation and php.

i have tried.

$data = $request->all();
$billinfo = $data[0]->total;
$productlist=$data[1];

i have also tried

$billinfo = $data[0]->object->total;

i have also tried

$billinfo = $data[0]->object[0]->total;

i have also tried

$billinfo = $data[0]->bill->total;

if i send the request again to the view like this:

$data = $request->all();
return json_encode($data);

And then print the response in consol .The consol gives following output:

Object {id: Array[2]}
>id:
>Array[2]
  0:Object
    card_number:"0"
    customer_id:"0"
    date:"06/18/2016"
    discount:"0"
    grandtotal:"563.5"
    payment_method:"cash"
    seller_id:""
    tax:"15"
    total:"490"
    __proto__:Object
  1:Array[2]
    0:Object
      stock_id:"5"
      subtotal:"244"
      unit:"2"
      unit_price:"122"
      __proto__:Object
    1:Object
      stock_id:"4"
      subtotal:"246"
      unit:"2"
      unit_price:"123"

My ajax sending function is

function ajaxsave(obj)
  {  
    var token=$('input[name=_token]').val();
    var baseUrl=document.getElementById("baseUrl").value;
    var url=baseUrl+"/sales/savecart";
    var id=obj;
    $.ajax({
      type: "GET",
      headers: {'X-CSRF-TOKEN': token},
      url:url,
      data: {id:id},
      datatype:'json',
      success: function(data) {

                var returndata =JSON.parse(data);
                console.log(returndata);
}
  });



  }

I am trying to learn laravel. this is my first project. Please Help !!!

I think this is the out put of Network XHR if i return var_dump($request->all())

 array(1) {
  ["id"]=>
  array(2) {
    [0]=>
    array(9) {
      ["customer_id"]=>
      string(1) "0"
      ["seller_id"]=>
      string(0) ""
      ["date"]=>
      string(10) "06/18/2016"
      ["payment_method"]=>
      string(4) "cash"
      ["card_number"]=>
      string(1) "0"
      ["total"]=>
      string(3) "244"
      ["discount"]=>
      string(1) "0"
      ["tax"]=>
      string(2) "15"
      ["grandtotal"]=>
      string(5) "280.6"
    }
    [1]=>
    array(1) {
      [0]=>
      array(4) {
        ["stock_id"]=>
        string(1) "5"
        ["unit"]=>
        string(1) "2"
        ["unit_price"]=>
        string(3) "122"
        ["subtotal"]=>
        string(3) "244"
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire