2016-03-04 4 views
2

У меня есть таблица html со списком автомобилей из моей базы данных, я хочу выбрать строку в таблице, чтобы открыть загрузочный модальный с правильным id из таблицы db. Кнопка «Изменить статус» открывает модальный показ автомобиля, номерной знак и статус. Но он извлекает только последний идентификатор. Когда я перемещаю модальную вершину, она выбирает первый идентификатор таблицы. Я не понимаю, что я могу сделать, чтобы исправить это.Laravel 5 Извлеките правильный идентификатор из таблицы

Вот мой HTML-код:

@if (isset($results)) 
<div class="table-responsive"> 
    <table class="table table-striped table-hover"> 
     <thead> 
      <tr> 
       <th width="15%"></th> 
       <th>#</th> 
       <th>Numberplate</th> 
       <th>Status</th> 
       <th>Added</th> 
       <th>Changed</th> 
       <th>Change</th> 
      </tr> 
     </thead> 
     @foreach ($results as $result) 
     <tbody> 
      <tr> 
       <td></td> 
       <td>{{ $result->id }}</td> 
       <td>{{ $result->LicencePlate }}</td> 
       <td>{{ $result->Status }}</td> 
       <td>{{ $result->created_at }}</td> 
       <td>{{ $result->updated_at }}</td> 
       <td> 
        <button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal">Change Status</button> 
       </td> 
      </tr> 
     </tbody> 
     @endforeach 
     @endif 
    </table> 
</div> 
@if (isset($cars)) 
<div class="table-responsive"> 
    <table class="table table-striped table-hover"> 
     <thead> 
      <tr> 
       <th width="15%"></th> 
       <th>#</th> 
       <th>Numberplate</th> 
       <th>Status</th> 
       <th>Added</th> 
       <th>Changed</th> 
       <th>Change</th> 
      </tr> 
     </thead> 
     @foreach ($cars as $car) 
     <tbody> 
      <tr> 
       <td></td> 
       <td>{{ $car->id }}</td> 
       <td>{{ $car->LicencePlate }}</td> 
       <td>{{ $car->Status }}</td> 
       <td>{{ $car->created_at }}</td> 
       <td>{{ $car->updated_at }}</td> 
       <td> 
        <button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal">Change Status</button> 
       </td> 
      </tr> 
      @endforeach 
     </tbody> 
    </table> 
</div> 
@endif 
</div> 
</div> 
</div> 
@if (isset($cars)) 
@foreach ($cars as $car) 
<!-- Modal --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> 
       </button> 
       <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
      </div> 
      <div class="modal-body"> 
       <p>ID: {{ $car->id }}</p> 
       <p>Numberplate: {{ $car->LicencePlate }}</p> 
       <p>Status: {{ $car->Status }}</p> 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="button" class="btn btn-primary">Save changes</button> 
      </div> 
     </div> 
    </div> 
</div> 
@endforeach 
@endif 

Не совсем уверен, если это специальный метод в моем контроллере или вы должны видеть мою модель. Если так, я могу отредактировать этот вопрос.

+1

петля должна быть внутри –

+0

@VinodVT Все те же результаты :( –

ответ

1

Теперь в вашем случае модальный идентификатор #myModal повторяется внутри цикла. Изменение модальный идентификатор и идентификатор кнопки, как,

<button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal_{{ $car->id }}">Change Status</button> 

И модальный

<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
     <!--Modal content here --> 
<div> 

FYI: Используйте одноконтурный как для кнопки и модальный.

+1

Вы, сэр, бог мне :) –

Смежные вопросы