0

I have a table in which a column stores json data,but I am unable to loop through it in the view.

Sample Data :

[  
   {  
      "id":28,
      "item_id":4,
      "item_name":"Texco Dx",
      "rate":"15.00",
      "unit":"box",
      "qty":"20",
      "price":300,
      "tax":12,
      "taxType":"GST",
      "tax_paid":36,
      "disc":0,
      "value":336,
      "barcode":"1234ergeg",
      "hsn":"q23423",
      "lot_number":"1235r23r",
      "checked":true,
      "error":false,
      "returnedQty":12,
      "returnedValue":33.6,
      "returnedTax":3.6,
      "allowedQty":10
   },
   {  
      "id":27,
      "item_id":3,
      "item_name":"Norflox Tz",
      "rate":"124.00",
      "unit":"qty",
      "qty":"10",
      "price":1240,
      "tax":10,
      "taxType":"GST",
      "tax_paid":124,
      "disc":0,
      "value":1364,
      "barcode":"11121231",
      "hsn":"123213",
      "lot_number":"123",
      "checked":true,
      "error":false,
      "returnedQty":3,
      "returnedValue":272.8,
      "returnedTax":24.8,
      "allowedQty":9
   }
]

This is how I am trying to loop through it :

    <table>
        <tr class="left">
            <th>SR NO</th>
            <th>ITEM NAME</th>
            <th>PRICE/UNIT</th>
            <th>QTY</th>
            <th>DISCOUNT %</th> 
            <th>TAX</th>
            <th>TOTAL</th>
        </tr>
        @foreach ($bill[0]->ITEM_DETAILS as $item)
        <tr>
            <td>{{$item->id}}</td>
            <td>{{$item0->item_name}}</td>
        </tr>
        @endforeach
    </table>

When I stored the same data using ng-init and tried iterating through it using ng-repeat it worked.

Can anyone explain this behaviour.

Thanks in advance

  • Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – aynber Aug 12 '19 at 20:13
  • https://www.php.net/manual/en/function.stripslashes.php – AbraCadaver Aug 12 '19 at 20:18
  • *How* do you loop through that JSON in the view, can you edit your question and post the code? – brombeer Aug 12 '19 at 20:18
  • Possible duplicate of [How to Decode Json object in laravel and apply foreach loop on that in laravel](https://stackoverflow.com/questions/29070907/how-to-decode-json-object-in-laravel-and-apply-foreach-loop-on-that-in-laravel) – zod Aug 12 '19 at 20:29
  • It's not a duplicate of any of the links provided here. None is helpful for me or I may be too lame to understand those. – Praveen Kumar Aug 12 '19 at 20:49
  • `{{ dd($item) }}` in your `foreach` to see if it contains the data you think it contains. Also missing a closing `` – brombeer Aug 12 '19 at 20:51
  • I am not even getting inside the foreach loop getting error as "Invalid argument supplied for foreach()" – Praveen Kumar Aug 12 '19 at 20:58
  • So `$bill[0]->ITEM_DETAILS` is not set – brombeer Aug 12 '19 at 21:11
  • the sample data provided is the value of $bill[0]->ITEM_DETAILS only. Some sort of conversion is required rest is fine. – Praveen Kumar Aug 12 '19 at 21:18

1 Answers1

0

If you are getting a json in the view and assuming that $bill contains the json:

<table>
    <tr class="left">
        <th>SR NO</th>
        <th>ITEM NAME</th>
        <th>PRICE/UNIT</th>
        <th>QTY</th>
        <th>DISCOUNT %</th>
        <th>TAX</th>
        <th>TOTAL</th>
    </tr>
    @foreach (json_decode($bill, true) as $item)
    <tr>
        <td>{{$item['id']}}</td>
        <td>{{$item['item_name']}}</td>
        <td>{{$item['price']}}</td>
        <td>...</td>
    </tr>
    @endforeach
</table>

If your json comes from the controller you could pass an array and do this.

In controller

$bills = ....
return view('your.template', ['bill' => json_decode($bills, true)]);

In view

<table>
    <tr class="left">
        <th>SR NO</th>
        <th>ITEM NAME</th>
        <th>PRICE/UNIT</th>
        <th>QTY</th>
        <th>DISCOUNT %</th>
        <th>TAX</th>
        <th>TOTAL</th>
    </tr>
    @foreach ($bill as $item)
    <tr>
        <td>{{$item['id']}}</td>
        <td>{{$item['item_name']}}</td>
        <td>{{$item['price']}}</td>
        <td>...</td>
    </tr>
    @endforeach
</table>
Ezequiel Fernandez
  • 954
  • 11
  • 18