Home  >  Q&A  >  body text

Display json type data in blade

Display json type data in the blade I have a table in my database that stores data and one of the data is of json type and everything is going fine but when I want to display these json in blade the problem starts This is my json data type [![Enter image description here][1]][1]

In my blade I use php to query, extract and display all the data with the same order id, but I want to display the json data while doing this

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

No data is displayed in the second foreach where I want to display the json data

edit:

In the second foreach where I want to show the json data, no data is shown to me, I think this is because I'm calling the data wrong because when doing the suggested action, it's not showing me anything

[{"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 days ago272

reply all(1)I'll reply

  • P粉936509635

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

    You need to use json_decode to convert the json array into an array of arrays (json_decode($json, true)) or an array of objects (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

    A better option is to define this behavior in the $cast property of the Eloquent model.

    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
    

    reply
    0
  • Cancelreply