【问题标题】:Dynamic number of rows in Laravel BladeLaravel Blade 中的动态行数
【发布时间】:2015-05-09 17:15:32
【问题描述】:

我想要这样的表中的动态行数。

number   name
1        Devy

这是我的 Blade 模板。

<thead>
        <th>number</th>
        <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $value)
        <tr>
            <td></td>
            <td>{{$value->name}}</td>
        </tr>
    @endforeach
</tbody>

我该怎么做?

【问题讨论】:

  • 你可以使用 name="myname[]"
  • 亲爱的开发者,我认为 {{ $loop->iteration }} 是最好的回答。

标签: php laravel blade


【解决方案1】:

试试$loop-&gt;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>

`

【讨论】:

  • 这必须是标准答案,没有任何 +1 破解。
【解决方案2】:

这是正确的:

    @foreach ($collection as $index => $element)
           {{$index}} - {{$element['name']}}
   @endforeach

而且你必须使用 index+1,因为 index 从 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 ?>

在你的情况下:

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

【讨论】:

  • 除非 $collection(或 $aaa)有一个自定义索引编号,例如记录 id 可能不是增量或过滤的。
  • 对于 laravel 5.3+ 使用this,更好的解决方案。
  • 如果您 sortby() 您的收藏,这将不起作用。 $index 将以错误的顺序显示,因为它在排序之前引用了集合项序列。 {{$loop->iteration}} 效果更好。
【解决方案3】:

使用计数器并在循环中增加其值:

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

【讨论】:

    【解决方案4】:

    使用 $loop 变量

    参考此链接Loop Variable

    【讨论】:

      【解决方案5】:

      从 Laravel 5.3 开始,这变得容易多了。只需在给定循环中使用 $loop 对象。您可以访问 $loop->index 或 $loop->iteration。检查这个答案:https://laracasts.com/discuss/channels/laravel/count-in-a-blade-foreach-loop-is-there-a-better-way/replies/305861

      【讨论】:

        【解决方案6】:

        只需在foreach() 之前取一个变量,例如$i=1。并在foreach() 结束之前增加$i。因此,您可以在所需的&lt;td&gt;&lt;/td&gt;echo $i

        【讨论】:

          【解决方案7】:

          尝试以下方法:

          <thead>
              <th>number</th>
              <th>name</th>
          </thead>
          <tbody>
              @foreach ($aaa as $index => $value)
                  <tr>
                      <td>{{$index}}</td>
                      <td>{{$value}}</td>
                  </tr>
              @endforeach
          </tbody>
          

          【讨论】:

          • 这个答案不正确。因为这个问题意味着别的东西。如果查询中存在“where”条件,则此编号将是错误的。
          【解决方案8】:

          您可以在 Blade 中像这样使用它。我希望这会有所帮助。

          <thead>
              <th>number</th>
              <th>name</th>
          </thead>
          <tbody>
              @foreach ($aaa as $index => $value)
                  <tr>
                      <td>{{$index +1}}</td>
                      <td>{{$value}}</td>
                  </tr>
              @endforeach
          </tbody>
          

          注意:{{$index +1}}因为$index从0开始

          【讨论】:

            【解决方案9】:

            Laravel 8 -

            <?php $serial = "1"; ?> - Before @foreach loop
            <td>{{ $serial++ }}</td> - Inside @foreach loop
            

            【讨论】:

              猜你喜欢
              • 2018-02-05
              • 2016-05-22
              • 2020-01-12
              • 2017-02-16
              • 1970-01-01
              • 2019-01-29
              • 2021-09-05
              • 2016-06-17
              • 2021-08-20
              相关资源
              最近更新 更多