Как получить идентификатор от итерации к модалу начальной загрузки? - PullRequest
0 голосов
/ 13 июня 2019

Мне нужно захватить идентификатор из итерации, но кажется, что он захватывает последний идентификатор, который, кажется, очевидно, Я использую модал начальной загрузки, который я даже не знаю, как они кодируют свои jQuerys. Таким образом, как я могу сделать, чтобы эта кнопка не захватила последний идентификатор?

    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#applications_{{$user->id}}" id="#applications_{{$user->id}}">View applications</button>

      <!-- Modal -->
      <div class="modal fade" id="applications_{{$user->id}}" role="dialog" >
        <div class="modal-dialog" >

          <!-- Modal content-->
          <div class="modal-content" style="width: 600px !important;">
            <div class="modal-header">
              {{-- <button type="button" class="close" data-dismiss="modal">&times;</button> --}}
              <h4 class="modal-title">Applications</h4>
            </div>
            <div class="modal-body">
                <div class="text-muted" style="float: left;padding-right: 10px">
                <table class="table table-hover"> 
                  <tr>
                    <th>Company name</th>
                    <th>Location</th>
                    <th>Program</th>
                    <th>Application</th>
                    <th>Actions</th>
                  </tr>
                   @foreach($user->post as $form)
                     <tr>
                        <td>{{$form->pivot->company_name}}</td>
                        <td></td>
                        <td>{{$form->pivot->id}}</td>
                        <td> 
/* this button is only capturing the last id from iteration */
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#application_{{$form->pivot->id}}" >View {{$form->pivot->file}}</button> </td>
                        <td></td>
                      </tr>  
                   @endforeach

                   {{$user->name}}

                 </table>
                 </div> 

            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>

        <!-- Modal -->
      <div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header">

              <h4 class="modal-title">Application</h4>
            </div>
            <div class="modal-body">
                <div class="text-muted" style="float: left;padding-right: 10px">
                        <div class="center image">
                           <img src="/storage/file/{{$form->pivot->file}}"  class="img-round" alt="User Image">
                        </div>    
                 </div> 

            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>

Ожидается, что изображение будет извлечено из соответствующего поста, но оно работает только с последним идентификатором, без ошибок.

1 Ответ

0 голосов
/ 14 июня 2019

Ваш второй модал находится вне foreach, тогда id="application_{{$form->pivot->id}}" работает только с последним значением, которое он принял в цикле. Чтобы сделать это с лезвием, вы можете повторить цикл и создать модальный для каждого идентификатора и изображения:

<!-- 2nd Modal -->
@foreach($user->post as $form)
<div class="modal fade" id="application_{{$form->pivot->id}}" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Application</h4>
            </div>
            <div class="modal-body">
                <div class="text-muted" style="float: left;padding-right: 10px">
                    <div class="center image">
                        <img src="/storage/file/{{$form->pivot->file}}"  class="img-round" alt="User Image">
                    </div>    
                </div> 
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
@endforeach
...