在我的laravel
-应用程序中,我创建了一个数组,如下所示:
$positions = Position::whereHas('jobs')
->with('jobs')
->get()
->groupBy('name')
->mapSpread(function ($position) {
return $position->jobs->where('job_status_id', '=', '2')->pluck('industry');
})
这返回一个数组,看起来像这样:(我做了一个dd($positions((
IlluminateSupportCollection {#1280 ▼
#items: array:9 [▼
"Engineer" => IlluminateSupportCollection {#1279 ▶}
"Analyst" => IlluminateSupportCollection {#1274 ▼
#items: array:1 [▼
0 => AppIndustry {#1294 ▶}
]
}
"Receptionist" => IlluminateSupportCollection {#1304 ▶}
"Programmer" => IlluminateSupportCollection {#1306 ▼
#items: array:2 [▶]
}
]
}
现在,我想在我的blade
视图中显示键值和键值具有的作业量——例如";编程器(2(";
我试着这样做:
@foreach($positions as $k => $v)
@foreach ($v as $key => $value)
{{ $value->name }}
@endforeach
@endforeach
但这只会返回行业名称,例如";IT";或";构造";(我从行业关系中得到(
那么我该如何做到这一点呢?
您尝试对集合使用count((方法
@foreach($positions as $k => $v)
<div>{{ $k }} ({{ $v->count() }})</div>
@endforeach
参考:https://laravel.com/docs/6.x/collections#method-计数
计算for循环中的元素,并在视图中这样显示
@foreach($positions as $k => $v)
@foreach ($v as $key => $value)
{{ $value->name }} {{ count($value->toArray()) }}
@endforeach
@endforeach
如果出现错误,请移除toArray()
。