【问题标题】:How to automatically number each row in a table in laravel?如何在laravel中自动为表格中的每一行编号?
【发布时间】:2021-04-08 13:20:07
【问题描述】:

当显示数据库中的数据时,我想自动为每一行编号。像这样的

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td>@fat</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>Larry</td>
      <td>the Bird</td>
      <td>@twitter</td>
    </tr>
  </tbody>
</table>

根据数据库中可用的数据,每行按从 1 到 n 的升序编号。我该如何正确地做到这一点。任何帮助请是 laravel 的新手

【问题讨论】:

  • 计数器怎么样
  • 请问你能帮我吗?

标签: php laravel laravel-8


【解决方案1】:

如果您使用刀片,laravel 自动生成的 $loop 变量可能会有所帮助。像这样:

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col">Handle</th>
    </tr>
  </thead>
  <tbody>
    @foreach($items as $item)
    <tr>
      <th scope="row">{{ $loop->iteration }}</th>
      <td>{{ $item->first_name }}</td>
      <td>{{ $item->last_name }}</td>
      <td>@{{ $item->username }}</td>
    </tr>
    @endforeach
  </tbody>
</table>

您可以在此处找到文档:https://laravel.com/docs/8.x/blade#the-loop-variable

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 2013-06-05
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-05-01
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多