Skip to content
Advertisement

How to add index in laravel blade html attribute while calling?

This is my controller code –

$date = $request->input('date');
$reports = CovidUserResponses::where('recorded_date_time', $date)->get();
$questions = CovidQuestions::orderBy('id')->get();;
return view('self-assessment.report')->with('reports', $reports)->with('questions', $questions)->with('date', $date);

This is my database –

 Schema::create('covid_questions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('question_title');
            $table->string('question_type');
            $table->decimal('question_no', 5, 1);
            $table->timestamps();
        });

 Schema::create('covid_user_responses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('question_1');
            $table->unsignedBigInteger('question_2');
            $table->unsignedBigInteger('question_3');
            $table->unsignedBigInteger('question_4');
            $table->unsignedBigInteger('question_5');
            $table->unsignedBigInteger('question_6');
            $table->unsignedBigInteger('question_7');
            $table->unsignedBigInteger('question_8');
            $table->unsignedBigInteger('question_9');
            $table->unsignedBigInteger('question_10');
            $table->boolean('status');
            $table->decimal('temperature', 5, 1);
            $table->date('recorded_date_time');
            $table->timestamps();
        });

This is my report.blade.php

@foreach($reports as $r)
     <tr>
       <td>{{ $r->recorded_date_time }}</td>
       <td>{{ $r->user->name }}</td>
       <td>{{ $r->temperature }}</td>
       <td><span class="badge badge-@if($r->status == 0)danger @else ($r->status == 1)primary @endif">@if($r->status == 1) Passed @else Failed @endif<span></td>
       <td>
          <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#showanswers-{{$r->id}}">
                Show Answers
           </button>                                          
       </td>
      </tr>
@endforeach

Whenever user click on show answers , it should display answers in bootstrap modal. For that below is the code ..

 @foreach($reports as $r)
     <div class="modal fade" id="showanswers-{{$r->id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
           <div class="modal-content">
              <div class="modal-header">
                 <h5 class="modal-title" id="exampleModalLabel">Answers of {{ $r->user->name }}</h5>
                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                 </button>
              </div>
              <div class="modal-body">
                  @foreach($questions as $q)
                     <ul class="list-group list-group-flush">
                        <li class="list-group-item">{{$q->question_no}} ) {{$q->question_title}}</li>
                        <li class="list-group-item">**{{$r->question_{{no++}}}}**</li>
                     </ul>
                  @endforeach
              </div>
              <div class="modal-footer">
                 <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              </div>
          </div>
       </div>
    </div>
@endforeach

In modal instead of

{{$r->question_{{no++}}}}

What should i pass here so that all questions should pick its increment and display correctly like question_1, question_2, question_3 so on..

now with the help of count variable it’s working fine

 @php
   $count = 0;
 @endphp
 @foreach($questions as $q)
    @php $count++;@endphp
    <ul class="list-group list-group-flush">
        <li class="list-group-item">{{$q->question_no}} ) {{$q->question_title}}</li>
        <li class="list-group-item">
            @if ( {{ $r->{'question_' . $count} }} == 1 ) yes @else no @endif
         </li> 
    </ul>
 @endforeach

but there is some mistake in this line @if ( {{ $r->{‘question_’ . $count} }} == 1 ) yes @else no @endif. Can anybody identify?

Advertisement

Answer

In your blade add $count,

<div class="modal-body">
        @php
         $count = 0;
       @endphp
       @foreach($questions as $q)
           @php $count++;@endphp
           <ul class="list-group list-group-flush">
               <li class="list-group-item">{{$q->question_no}} ) {{$q->question_title}}</li>
               <li class="list-group-item">**{{ $r->{'question_' . $count} }}**</li>
           </ul>
       @endforeach
</div>

You can refer more about Get PHP class property by string

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement