【问题标题】:How to get key of the item in laravel collection?如何获取 laravel 集合中项目的密钥?
【发布时间】:2015-06-24 12:36:06
【问题描述】:

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

【问题讨论】:

  • 显示你的刀片代码

标签: laravel


【解决方案1】:

没有必要把事情复杂化。

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

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

【讨论】:

  • helo 不用foreach也能拿到key吗?
  • $collection = Tracking::confirmedNotDel() ->groupBy('oppor_id') ->selectRaw('oppor_id, sum(logged_mins)/60 as logged_hours') ->get(); $collection = 收集($collection); $data = $collection->where('oppor_id', $oppr->id); dd($数据); //该对象的键,因为我想获取行号
猜你喜欢
  • 1970-01-01
  • 2015-04-21
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2018-06-26
  • 1970-01-01
相关资源
最近更新 更多