【问题标题】:Looping through json laravel循环通过 json laravel
【发布时间】:2019-12-19 10:00:42
【问题描述】:

我有一个表,其中有一列存储 json 数据,但我无法在视图中循环遍历它。

样本数据:

[  
   {  
      "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
   }
]

这就是我尝试循环的方式:

    <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>

当我使用 ng-init 存储相同的数据并尝试使用 ng-repeat 对其进行迭代时,它起作用了。

谁能解释一下这种行为。

提前致谢

【问题讨论】:

标签: php laravel-5 laravel-blade


【解决方案1】:

如果您在视图中获取 json 并假设 $bill 包含 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>

如果您的 json 来自控制器,您可以传递一个数组并执行此操作。

在控制器中

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

在视图中

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多