我希望像这样的表中的动态行数.
number name
1 Devy
Run Code Online (Sandbox Code Playgroud)
这是我的Blade模板.
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
@foreach ($aaa as $value)
<tr>
<td></td>
<td>{{$value->name}}</td>
</tr>
@endforeach
</tbody>
Run Code Online (Sandbox Code Playgroud)
我怎么做?
Ken*_*ngi 15
尝试$loop->iteration变量。
`
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
@foreach ($aaa as $value)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$value}}</td>
</tr>
@endforeach
</tbody>
Run Code Online (Sandbox Code Playgroud)
`
Adn*_*nan 10
这是对的:
@foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
@endforeach
Run Code Online (Sandbox Code Playgroud)
并且您必须使用索引+ 1,因为索引从0开始.
在视图中使用原始PHP不是最佳解决方案.例:
<tbody>
<?php $i=1; @foreach ($aaa as $value)?>
<tr>
<td><?php echo $i;?></td>
<td><?php {{$value->name}};?></td>
</tr>
<?php $i++;?>
<?php @endforeach ?>
Run Code Online (Sandbox Code Playgroud)
在你的情况下:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
@foreach ($aaa as $index => $value)
<tr>
<td>{{$index}}</td> // index +1 to begin from 1
<td>{{$value}}</td>
</tr>
@endforeach
</tbody>
Run Code Online (Sandbox Code Playgroud)
使用计数器并在循环中递增其值:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
<?php $i = 0 ?>
@foreach ($aaa as $value)
<?php $i++ ?>
<tr>
<td>{{ $i}}</td>
<td>{{$value->name}}</td>
</tr>
@endforeach
</tbody>
Run Code Online (Sandbox Code Playgroud)