Laravel 来自 collection 的嵌套 Foreach 循环

Laravel Nested Foreach loop from collection

我有以下 collection:

截图: 我想做的是 return 每个州立大学使用 blade 模板按州划分为 3 行。例如:

马萨诸塞州

学院1学院2学院3

佛罗里达

学院1学院2学院3

大学 4

...

我已经尝试了很多其他帖子中建议的示例,但未能找到解决方案。

这是我尝试过的:

Here is what I have tried:

     @foreach ($colleges as $college)
      @foreach($college as $state)
        <h1 style="center">{{ $state[0]['College']['state'] }}</h1>
      <ul class="nospace clear">
        <li class="one_quarter first">
          <a href="#">
            <img src="{{ asset('storage/' . $state[0]['College']['logo'])  }}" alt="{{ $state[0]['College']['college'] }}" />
          </a>
          <h3 class="center">{{ $state[0]['College']['college'] }}</h3>
        </li>
      </ul>
      @endforeach
@endforeach

如有任何帮助,我们将不胜感激。

格雷格

从你的合集截图来看,结构如下:

[
  state
    college
    college

  state
    college
    college
    college

  ...
]

因此,如果您 foreach 遍历 $states 集合,将 $key 作为州名称,将 $value 作为该州的大学集合,您可以以下:

@foreach ($states as $state => $colleges)
    <h1 style="center">{{ $state }}</h1>

    <ul class="nospace clear">
        @foreach ($colleges as $college)
            <li class="one_quarter first">
                <a href="#">
                    <img src="{{ asset('storage/' . $college->logo) }}" alt="{{ $college->college }}" />
                </a>
                <h3 class="center">{{ $college->college }}</h3>
            </li>
        @endforeach
    </ul>
@endforeach