如何自动给laravel中的table中的每一行编号?
How to automatically number each row in a table in laravel?
我想在显示数据库中的数据时自动为每一行编号。像这样
<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
中的新人
如果您使用的是 blade,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
我想在显示数据库中的数据时自动为每一行编号。像这样
<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
中的新人如果您使用的是 blade,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