如何在laravel集合中获取项目的关键字?

zar*_*cel 3 laravel

我将一个集合传递给刀片模板引擎,并在那里迭代它.我想在数组中显示项目位置.我想过使用items key,如何从集合项中提取它?

cee*_*yoz 11

无需过度复杂化.

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

// in your view
@foreach($collection as $index => $item)
  {{ $index }}
@endforeach
Run Code Online (Sandbox Code Playgroud)