Laravel增量id

时间:2018-04-11 06:52:35

标签: php laravel eloquent

我有5 foreach秒。那么我如何增加我喜欢

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

查看

@foreach($questions as $question)
 {{ $i }}
@endforeach

@foreach($inquiries as $question)
 {{ $i }}
@endforeach

@foreach($queries as $question)
 {{ $i }}
@endforeach

@foreach($examinations as $question)
 {{ $i }}
@endforeach

@foreach($inquisitions as $question)
 {{ $i }}
@endforeach

4 个答案:

答案 0 :(得分:1)

你应该尝试以下方式。

<?php 
$i = 0;
?>
@foreach($questions as $question)
 {{ $i++ }}
@endforeach

@foreach($inquiries as $question)
 {{ $i++ }}
@endforeach

@foreach($queries as $question)
 {{ $i++ }}
@endforeach

@foreach($examinations as $question)
 {{ $i++ }}
@endforeach

@foreach($inquisitions as $question)
 {{ $i++ }}
@endforeach

答案 1 :(得分:1)

@php
  $i = 0;
@endphp
@foreach($questions as $question)
 {{ $i }}
  $i++;
@endforeach

在每个foreach循环中使用$ i作为增量$ i ++

答案 2 :(得分:1)

如果希望你的$ i在每次循环后改变,你应该通过引用传递变量。检查以下链接中的文章: 链接:http://php.net/manual/en/language.references.pass.php

答案 3 :(得分:0)

您可以使用专用于此的刀片变量:

@foreach($questions as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($inquiries as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($queries as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($examinations as $question)
    {{ $loop->iteration }}
@endforeach

@foreach($inquisitions as $question)
    {{ $loop->iteration }}
@endforeach

Blade docs