Home > database >  display json type data in a blade
display json type data in a blade

Time:12-28

display json type data in a blade I have a table in my DB where I store data and one of those data is of the json type, and everything goes well for me, but the problem starts when I want to display those json in a blade this is my json data type [![enter image description here][1]][1]

where in my blade I make the query with php that all the data that has the same id of an order is extracted and displayed but I want to display the json data where I do this foreach

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

in the second foreach where I want to show the json data does not show me any data

EDIT:

in the second foreach where I want to show the json data does not show me any data y creo que es porque llamo mal los datos porque al hacer lo recomendado no me muestra nada

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

CodePudding user response:

You need to either use json_decode to turn that 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)
            <h1>{{ $documen->partidas_id }}</h1>
        @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)
            <h1>{{ $documen['partidas_id'] }}</h1>
        @endforeach
    @endif
@endforeach

A better option would be to define that behavior in the $cast property of an 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

CodePudding user response:

If you use the Model and that column is of type json instead of using the DB: query builder the column will be seen as an array inside Laravel (and the blade)

Then you can just treat it like a standard array.

@foreach($tareasco as $audi)
   <!-- your looped items -->
@endforeach

I find its good practice to rely on the Eloquent and Models

CodePudding user response:

If you need to access it as JSON in foreach, you should access it like an Object, not an array.

<h1>{{$documen->partidas_id}}</h1>

For Your Information

When we use Laravel Framework, we don't write fetch commands in view. like this

<?php
    $order[] = $ord_com->id;
    $tareasco = DB::table('tareas')->whereIn('orden_compra_id',$order)->orderBy('created_at','desc')->get();
?> 

We use MVC (Model View Controllr ) architecture to manipulate data.

  • Related