Looping through collections -Laravel

前端 未结 2 647
广开言路
广开言路 2021-01-26 22:12

I was trying to loop through a collection based on the key

What I am trying to accomplish here is to group each company based on the alphabet in my view

相关标签:
2条回答
  • 2021-01-26 22:58
    foreach($results as $alphabet => $collection) {
      dump($alphabet, $collection);
    }
    
    0 讨论(0)
  • 2021-01-26 23:12

    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

    0 讨论(0)
提交回复
热议问题