Home > Software engineering >  read object of object data laravel blade
read object of object data laravel blade

Time:07-22

data looks like this

[
    {
      "id": 22528,
      "user_id": 25273,
      "non_sql": "{\"data\": {\"data_1\": \"4\", \"data_2\": \"5\", \"data_3\": \"4\", \"data_4\": \"5\"}}",
      "created_at": "2022-06-12T04:38:36.000000Z",
      "updated_at": "2022-06-12T04:40:10.000000Z",
    },
]

when tried

 <td>{{ $item["non_sql"] ??  'NA' }}</td>

it gives me

{"data": {"data_1": "4", "data_2": "5", "data_3": "4", "data_4": "5"}}

I just need to return

data_1: 4
data_2: 5
data_3: 4
data_4: 5

I tried

<td>{{ json_decode($item["non_sql"]) ??  'NA' }}</td>

and have error htmlspecialchars(): Argument #1 ($string) must be of type string, stdClass given

CodePudding user response:

You can try this way

{{ implode(",",json_decode($item["non_sql"])) ?? 'NA' }}

CodePudding user response:

Before you send that result you need to json_decode that non_sql

Just do it before sending it to view

foreach ($result as $k => $v) { 
    $result[$k]["non_sql"] = json_decode($v["non_sql"], true);    
}
return view( 'some-template', [ 'result' => $result] );

To loop it in laravel blade you need to foreach non_sql.data so :

@foreach ($result as $item)    
    @foreach ($item["non_sql"]["data"] as $value)
        {{$value}}
    @endforeach
@endforeach
  • Related