Laravel 4提供计数变量时从foreach循环中添加数字? [英] Laravel 4 adding numbers from foreach loop when count variable is supplied?

查看:75
本文介绍了Laravel 4提供计数变量时从foreach循环中添加数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数组$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆