首页  >  问答  >  正文

在刀片中显示json类型数据

在刀片中显示json类型数据 我的数据库中有一个表,用于存储数据,其中一个数据是 json 类型,一切都很顺利,但是当我想在刀片中显示这些 json 时,问题就开始了 这是我的 json 数据类型 [![在此处输入图像描述][1]][1]

在我的刀片中,我使用 php 进行查询,提取并显示具有相同订单 id 的所有数据,但我想在执行此操作时显示 json 数据

<div class="modal-body">
    <?php
    $order[] = $ord_com->id;
    $tareasco = DB::table('tareas')->whereIn('orden_compra_id',$order)->orderBy('created_at','desc')->get();
    ?> 
    @foreach($tareasco as $audi)
        {{ $audi->componente_id }}<!--here it shows me everything ok-->
        @if (is_array($audi->componente_id) || is_object($audi->componente_id))
            @foreach ($audi->componente_id as $documen)
                <h1>{{$documen['partidas_id']}}</h1>
            @endforeach
        @endif
    @endforeach
</div>

在我想显示 json 数据的第二个 foreach 中没有显示任何数据

编辑:

在我想显示 json 数据的第二个 foreach 中,没有向我显示任何数据,我认为这是因为我调用数据错误,因为在执行建议的操作时,它没有向我显示任何内容

[{"id": 9913, "cantidad": "12", "costoini": "12", "partidas_id": "1", "servicios_id": "1077", "componente_id": "1", "sub_componente_id": "1", "sub_sub_componentes_id": "1"}] [{"id": 2548, "cantidad": "2", "costoini": "123", "partidas_id": "1", "servicios_id": "1077", "componente_id": "1", "sub_componente_id": "1", "sub_sub_componentes_id": "1"}, {"id": 7555, "cantidad": "4", "costoini": "124", "partidas_id": "2", "servicios_id": "1078", "componente_id": "1", "sub_componente_id": "1", "sub_sub_componentes_id": "1"}]

P粉064448449P粉064448449185 天前274

全部回复(1)我来回复

  • P粉936509635

    P粉9365096352024-03-23 11:15:34

    您需要使用 json_decode 将该 json 数组转换为数组数组 (json_decode($json, true)) 或对象数组 (json_decode($json))

    @php($tareasco = DB::table('tareas')->whereIn('orden_compra_id',$order)->orderBy('created_at','desc')->get())
    @foreach($tareasco as $audi)
        @php($componente_id = json_decode($audi->componente_id))
        @if (is_array($audi->componente_id))
            @foreach ($audi->componente_id as $documen)
                

    {{ $documen->partidas_id }}

    @endforeach @endif @endforeach
    @php($tareasco = DB::table('tareas')->whereIn('orden_compra_id',$order)->orderBy('created_at','desc')->get())
    @foreach($tareasco as $audi)
        @php($componente_id = json_decode($audi->componente_id, true))
        @if (is_array($audi->componente_id))
            @foreach ($audi->componente_id as $documen)
                

    {{ $documen['partidas_id'] }}

    @endforeach @endif @endforeach

    更好的选择是在 Eloquent 模型的 $cast 属性中定义该行为。

    class Tarea extends Model
    {
        protected $casts = [
            'componente_id' => 'array'
        ];
    }
    
    $tareasco = Tarea::whereIn(...)->orderBy(...)->get();
    
    @foreach($tareasco as $audi)
        @if (is_array($audi->componente_id))
            @foreach($audi->componente_id as $documen)
                {{ $documen['partidas_id'] }}
            @endforeach
        @endif
    @endforeach
    

    回复
    0
  • 取消回复