【问题标题】:Getting info from json column in laravel 5.5从 laravel 5.5 中的 json 列获取信息
【发布时间】:2020-11-06 23:21:47
【问题描述】:

我有 json 列,我在其中保存订单数据,下面的代码是该数据的示例:

[{"id":27,"name":"new product","price":7246,"quantity":"1","attributes":[],"conditions":[]}]

如果我的编辑页面中有这样的循环,我可以使用上面的代码获取订单信息:

@foreach($order->product_data as $data)
{{ $data['quantity'] }}
@endforeach

直到这里一切都很好,但是当我的订单具有属性 "attributes":[], 时出现问题,然后我收到此错误:

为 foreach() 提供的参数无效

我在 json 列中的数据是这样的:

"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":\"1\",\"attributes\":[{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]"

如您所见,此订单有 2 个属性。 (可多可少)。

附加

这就是我在订单模型中获取这些数据的方式:

protected $casts = [
    'product_data' => 'array',
  ];

知道我为什么会收到该错误,以及无论我的订单是否有属性,我如何获取我的属性数据?

更新

如果我打开没有属性和dd 的订单编辑页面,结果将是:

array:6 [▼
  "id" => 27
  "name" => "new product"
  "price" => 7246
  "quantity" => "1"
  "attributes" => []
  "conditions" => []
]

如果在带有属性的订单上加载相同的内容,则会出现上述错误。

【问题讨论】:

  • 你能告诉我们你对属性做了什么吗??
  • 什么意思?你需要看什么?这样我就可以给你了
  • 您遇到错误"attributes":[]的原因,您对此做了什么,显示给出此错误的代码
  • @Sohel0415 我什至一个字都没做!基于我的循环(我分享的问题)直接给出了这个错误。
  • 在你看来如何访问attributes[]

标签: php json laravel


【解决方案1】:

您应该只允许打印数据(如果存在并且具有我们期望的格式),请在您的代码中添加必要的条件,如下所示,这可能会防止出现错误

假设您在帖子中拥有的数组是您需要打印的数据。

@if($order->product_data) // check product_data value or not , also add key exists if needed
    @if(is_array($order->product_data)) // check for product_data is array or not
        @foreach($order->product_data as $data)
            {{ $data['quantity'] // if array }}
            {{ $data->quantity // if object }}
            @if($data->attributes) 
                @foreach($data->attributes as $attribute) // loop through attributes                    
                    {{ $attribute->nameOfTheProperty // if object }}
                @endforeach        
            @endif            
        @endforeach
    @endif
@endif

但如果可能,最好在控制器中处理数据

更新

您需要将数据转换为数组,因为如果没有值,您的属性属性将是空白数组,否则它将是 stdObject。

所以您的代码应该是:($order->product_data 应该是一个数组,如果不是,则将其转换为数组)

@if($order->product_data) 
    @if(is_array($order->product_data))  // assuming array
        @foreach($order->product_data as $data)
            {{ $data['quantity'] // if array }}            
            @if(count($data['attributes']) > 0) // **checked for blank array**
                @foreach($data->attributes as $attribute) // loop through attributes                    
                    {{ $attribute['attr']['label'] }}
                    {{ $attribute['attr']['price'] }}
                @endforeach        
            @endif            
        @endforeach
    @endif
@endif

【讨论】:

  • 这会解决错误,但我仍然不知道如何获取属性?
  • 第二期:订单without attribute是退货数量,订单with attribute什么都不退货。
  • 更新了我的代码以获取attributes。您需要在访问attributes之前添加这些特定条件
  • 什么也不返回。 :\
  • 我刚刚解释了如何处理,粘贴这段代码将无法直接工作。您需要根据输入的数据类型和您的要求调整条件
【解决方案2】:

尝试使用您的示例数据,并且执行时没有任何错误。

$json = "[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":\"1\",\"attributes\":[{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]";

$jsonAsArray = json_decode($json, true);

foreach ($jsonAsArray as $data) {
    echo $data['quantity'] . "\n";

    if (! empty($data['attributes']) ) {
        if (is_array($data['attributes'])) {
            foreach ($data['attributes'] as $attribute) {
                echo $attribute['attr']['label'] . ' ' . $attribute['attr']['price'] . "\n";
            }
        }
    }
}

输出:

1

Red 5000.00

22" 900000.00

在您的情况下,product_data 未转换为数组。 您可以尝试在 Order 模型中定义 Accessor,并删除 product_data 的 cast 属性

public function getProductDataAttribute($value)
{
    return json_decode($value, TRUE);
}

或手动解码循环中的 json

@foreach (json_decode($order->product_data, TRUE) as $data)
    {{ $data['quantity'] }}

    @if (! empty($data['attributes']))

        @foreach ($data['attributes'] as $attribute)

            {{ $attribute['attr']['label'] }} {{ $attribute['attr']['price'] }}

        @endforeach

    @endif

@endforeach

【讨论】:

  • 好的,我在我的模型中添加了public function getProductDataAttribute($value) 现在我该怎么办?
  • 尝试使用您在问题中使用的原始foreach 循环访问订单的$order->product_data
  • 我现在有这个@foreach($order->product_data as $data) @foreach($data['attributes'] as $attribute) {{ $attribute->price }} @endforeach @endforeach 没有错误,但也没有打印。
  • 错误是什么? JSON 被转换成数组,你正在访问它作为对象 {{ $attribute->price }} 应该是 {{ $attribute['attr']['price'] }}
  • 没有错误。什么都不会打印!等等我分享一些截图。
【解决方案3】:

这就是我的做法(类别是数据库中的 json 列):

    <div class="category">
     <ul class="list-group">
    @foreach ( json_decode( $code->category ) as $category)
    <li class="list-group-item d-flex justify-content-between align-items-center">
        <span class="badge badge-secondary badge-pill">{{$category}}</span>
    </li>
    @endforeach
</ul>
 </div>

您只需对您的 json 列进行 json_decode 即可获得一个数组并对其进行迭代。就是这样,干杯。

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 2018-02-20
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2016-10-05
    相关资源
    最近更新 更多