本文介紹了循環(huán)遍歷集合-Laravel的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我試圖遍歷基于鍵的集合
我在這里想要完成的是根據(jù)我認為的字母表對每個公司進行分組.
What I am trying to accomplish here is to group each company based on the alphabet in my view.
我如何遍歷這個??
$results = $companies->sortBy('name')->groupBy(function ($item, $key) {
return substr($item['name'], 0, 1);
});
dump($results);
控制器
推薦答案
作為@msonowal 答案的替代方案,您還可以使用 each():
As an alternative to @msonowal's answer you can also use each():
$results->each(function ($collection, $alphabet) {
dump($alphabet, $collection);
});
但是,如果您要在刀片文件中遍歷它們,您可以使用:
However, if you're going to loop through them in a blade file you would use:
@foreach ($results as $alphabet => $collection)
{!! dump($alphabet, $collection) !!}
@endforeach
https://laravel.com/docs/master/blade#loops
這篇關于循環(huán)遍歷集合-Laravel的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!