Reputation: 1563
I had an for loop inside that will display an indexed array from the results column that saves an array in a DB, inside a forelse loop.
This is how I store the array
$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
]);
Now I will retrieve all these data in my view using blade.
The array from the db is exactly like this when I use json_decode for the results field
[
"Result1",
"Result2",
]
This is what I have done so far.
@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
But it returns me an output of this in the view.
Test result for this sub method result is Result1 Result2
Test result for this sub method result is Result1 Result2
I have also tried this code below:
@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
But now it returns me this in my view
Test result for this sub method result is [
Test result for this sub method result is [
The output is supposed to be like these:
Test result for this sub method result is Result1
Test result for this sub method result is Result2
The problem here is that it returns me the whole value of the array that it should return each value of the array stored in the database.
Appreciate if someone could help. Thanks in advance.
Upvotes: 2
Views: 510
Reputation: 9853
This might work for you
$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
Upvotes: 1
Reputation: 4365
Try this:
@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
Upvotes: 1