Monday, February 27, 2017

Laravel PHP5 Input:All() Malformed Array With Undefined Offset

really simple question/task just have some sort of error somewhere that I can't figure out.

So I am trying to do a post request from a chrome extension (using jquery $.ajax) to a Laravel application.

Everything works except Laravel is not handling POST request payload well.

My ajax:

 $.ajax
    ({
        type: "POST",
        url: 'http://ift.tt/2mynDjK',
        dataType: 'json',
        //json object below
        data: arrayofproductsandthereassociates,
        success: function () {
  console.log(date);

arrayofproductsandthereassociates is populated, looks like this:

var arrayofproductsandthereassociates  = JSON.stringify({"Date":date,"Item":itemName,"Sold":sold,"Void":v0id,"Comp":comp,"Price":price,"Cost":cost,"Gross":gross,"Comps":comps,"Total Tax":totaltax,"Net":net,"Gross Proft":grossprofit,"Category":category});

When Laravel gets this payload using this controller code:

           $input = Input::all();
           print_r($input);
           $itemz = json_decode($input[0]);
           $model = new DATA;
           foreach($itemz as $key => $value) 
           {
           $model->$key = $value;      
           }
           $model-save();
           return;

It errors on the json_decode($input[0]); line, saying undefined offset.

The array looks all messed up at the print_r()

Output:

Array ( [{"Date":"09-28-2016","Item":"Reuben_Hot","Sold":"1","Void":"0","Comp":"0","Price":"$2_35","Cost":"$0_00","Gross":"$2_79","Comps":"$0_00","Total_Tax":"$0_44","Net":"$2_35","Gross_Proft":"$2_35","Category":"Soups_] => [Sandwiches"}] => )

How do I solve this? My sense is that the unmatched => in the array is the reason why I have an undefined offset? But how do I fix that?

Thank you!



via Summer Developer

Advertisement