2

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou! That worked! Thanks for the time. Appreciate it
glad it works although didn't tested it and thank you too :)
@Sohel0415 just a suggestion, adding isset to {{ $result_datas[$index] }} will avoid any crashes :)
@JayzdeVera Maybe the ul inside the forelse should be outside forelse, only li should be added inside the loop to have one list
1

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

8 Comments

Now it duplicates the submethod name or it is being looped twice but the value of the results is being looped or iterate nicely. This is the example or output: Test result for this sub method1 result is Result1 Test result for this sub method1 result is Result2 Test result for this sub method2 result is Result1 Test result for this sub method2 result is Result2
can you share how you are retrieving data for $result->request->methodology->submethods @JayzdeVera
this is the data: App\Models\SubMethodologies {#1297 id: 3, name: "Unit Test", created_at: "2018-04-04 16:42:08", updated_at: "2018-04-04 16:42:08", pivot: Illuminate\Database\Eloquent\Relations\Pivot {#1278 methodologies_id: 1, sub_methodologies_id: 3, created_at: "2018-04-04 16:42:08", updated_at: "2018-04-04 16:42:08", },
how do you want the data to be displayed instead of Test result for this sub method1 result is Result1 Test result for this sub method1 result is Result2 Test result for this sub method2 result is Result1 Test result for this sub method2 result is Result2
It supposed to be like: Test result for this sub method1 result is Result1 Test result for this sub method2 result is Result2 Sorry for the bad explanation
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.