我有一个for循环,它将显示结果列中的索引数组,该数组将数组保存在DB中,在forelse循环中。
这是我存储数组的方式
$final_result = ResultRecordFile::create([
'request_id' => $request->id,
'date_released' => $date,
'lab_access_code' => $input['lab_access_code'],
'remarks' => $input['remarks'],
'results' => json_encode($input['results']) // Stores array in the column
]);
现在我将使用刀片在我的视图中检索所有这些数据。
当我使用 json_decode 作为结果字段时,db中的数组就是这样的
[
"Result1",
"Result2",
]
这是我到目前为止所做的。
@forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
@foreach(json_decode($result->results) as $value)
{{ $value }}
@endforeach
</li>
</ul>
@empty
<p>This request has no submethods</p>
@endforelse
但是它在视图中返回了我的输出。
Test result for this sub method result is Result1 Result2
Test result for this sub method result is Result1 Result2
我也尝试过以下代码:
@forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
@for($i=0; $i < count($result->results); $i++)
{{ $result->results[$i]}}
@endfor
</li>
</ul>
@empty
<p>This request has no submethods</p>
@endforelse
但现在它在我的观点中将此归还给我
Test result for this sub method result is [
Test result for this sub method result is [
输出应该是这样的:
Test result for this sub method result is Result1
Test result for this sub method result is Result2
这里的问题是它返回数组的整个值,它应该返回存储在数据库中的数组的每个值。
感谢有人可以提供帮助。 提前谢谢。
答案 0 :(得分:1)
试试这个:
@forelse ($result->request->methodology->submethods as $submethod)
<ul>
@foreach(json_decode($result->results) as $value)
<li>
<b>{{ $submethod->name }}</b> result is
{{ $value }}
</li>
@endforeach
</ul>
@empty
<p>This request has no submethods</p>
@endforelse
答案 1 :(得分:1)
这可能适合你
$result_datas = json_decode($result->results);
$index = 0;
@forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
@if(isset($result_datas[$index]))
{{ $result_datas[$index] }}
@endif
<?php $index++;?>
</li>
</ul>
@empty
<p>This request has no submethods</p>
@endforelse