Laravel集合:按项目分组,并在视图中使用WhereIn


Laravel collections: GroupBy items and use WhereIn in the view

我正在从数据库中检索项目,并按"MONTH"进行分组:

$events          = DB::table('events')->get();
$events_by_month = $events->groupBy('month');

这返回一个集合,如下所示:

Collection {#186 ▼
  #items: array:1 [▼
    "DEC" => Collection {#159 ▼
      #items: array:1 [▼
        0 => {#166 ▼
          // attributes
        }
      ]
    },
     "JAN" => Collection {#159 ▼
      #items: array:1 [▼
        0 => {#166 ▼
          // attributes
        }
      ]
    }
  ]
}

我将其传递到视图中,以便在其中显示给定月份的项目。我可以在这里使用whereIn吗(在这种情况下,关键是什么?(或者有其他/更好的方法吗?

whereIn无钥匙返回错误:

@foreach($mths->whereIn(['DEC', 'JAN']) as $mth => $events)
 $events_by_month=DB::table('events')->get()->groupBy('month');

在视图中:

   @foreach($events_by_month->where(['month'=>'DEC') as $month=>$events)
       {{ $month }} 
       @foreach($events as $event)
         {{ $event }} 
       @endforeach
   @endforeach