在刀片中顯示json類型數(shù)據(jù) 我的資料庫中有一個表,用於存儲數(shù)據(jù),其中一個數(shù)據(jù)是 json 類型,一切都很順利,但是當(dāng)我想在刀片中顯示這些 json 時,問題就開始了 這是我的 json 資料類型 [![在此輸入影像描述][1]][1]
在我的刀片中,我使用 php 進(jìn)行查詢,提取並顯示具有相同訂單 id 的所有數(shù)據(jù),但我想在執(zhí)行此操作時顯示 json 數(shù)據(jù)
<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 數(shù)據(jù)的第二個foreach 中,沒有向我顯示任何數(shù)據(jù),我認(rèn)為這是因?yàn)槲艺{(diào)用數(shù)據(jù)錯誤,因?yàn)樵趫?zhí)行建議的操作時,它沒有向我顯示任何內(nèi)容
[{"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"}]
您需要使用json_decode
將該json 陣列轉(zhuǎn)換為陣列陣列(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