laravel 如何获取laravel集合中项目的密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29702933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:22:14  来源:igfitidea点击:

How to get key of the item in laravel collection?

laravel

提问by zarcel

I pass a collection to the blade templating engine and in there I iterate over it. I want to display items position in the array. I thought of using items key, how do I extract it from the collections items?

我将一个集合传递给刀片模板引擎,然后在那里迭代它。我想显示数组中的项目位置。我想到了使用 items 键,如何从集合项中提取它?

回答by ceejayoz

No need to overcomplicate things.

没有必要把事情复杂化。

// in your controller
return view()->with('collection', $collection);

// in your view
@foreach($collection as $index => $item)
  {{ $index }}
@endforeach