提供计数变量时,Laravel 4 从 foreach 循环中添加数字? [英] Laravel 4 adding numbers from foreach loop when count variable is supplied?
问题描述
我将数组 $cats 传递给我的 laravel 模板视图.它是来自数据库事务的多维数组,包含类别数据.所以它将包含如下数据:
I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
等等.在我的刀片模板中,我有以下代码:
And so on. In my blade template I have the following code:
{{ $i=0 }}
@foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
@endforeach
输出:
0 First Category
1 Second Category
2 Third Category
注意类别名称前面的数字.他们来自哪里?这是一些聪明的 Laravel 技巧吗?似乎当您包含计数器变量时,它们会自动添加.我在任何地方都找不到任何提及它,我不想要它们!我该如何摆脱它们?
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
谢谢.
推荐答案
你只需要使用纯php翻译即可:
You just need to use the plain php translation:
@foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
@endforeach
注意$index
将从0
开始,所以应该是{{ $index+1 }}
Note the $index
will start from 0
, So it should be {{ $index+1 }}
这篇关于提供计数变量时,Laravel 4 从 foreach 循环中添加数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!