【问题标题】:Laravel 5 Fetch the right id from tableLaravel 5 从表中获取正确的 id
【发布时间】:2016-06-17 23:33:23
【问题描述】:

我有一个 html 表,其中包含我的数据库中的汽车列表,我希望在表中选择一行以从 db 表中打开具有正确 id 的引导模式。 “更改状态”按钮打开显示汽车、车牌和状态的模式。但它只获取最新的 id。当我将模式移到顶部时,它会获取表格上的第一个 id。我很困惑我能做些什么来解决这个问题。

这是我的 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

不完全确定我的控制器中是否有特殊方法或者您需要查看我的模型。如果是这样,我可以编辑这个问题。

【问题讨论】:

  • 循环应该在
  • @VinodVT 结果还是一样 :(

标签: php html laravel


【解决方案1】:

现在,在您的情况下,模态 ID #myModal 在循环内重复。更改模态 id 和按钮 id 之类的,

<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>

仅供参考:对按钮和模式都使用单个循环。

【讨论】:

  • 先生,你是我的神 :)
猜你喜欢
相关资源
最近更新 更多
热门标签