Php Laravel集合:按项目分组并在视图中使用where
我正在从数据库中检索项并按“月”对它们进行分组:Php Laravel集合:按项目分组并在视图中使用where,php,laravel,Php,Laravel,我正在从数据库中检索项并按“月”对它们进行分组: $events = DB::table('events')->get(); $events_by_month = $events->groupBy('month'); 这将返回一个集合,如下所示: Collection {#186 ▼ #items: array:1 [▼ "DEC" => Collection {#159 ▼ #items: array:1 [▼ 0
$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
}
]
}
]
}
我将把它传递给视图,在那里我要显示给定月份的项目。我可以在这里使用其中的(哪种情况下关键是什么?)还是有其他/更好的方法
中没有键,其中
返回错误:
@foreach($mths->whereIn(['DEC', 'JAN']) as $mth => $events)
并且认为:
@foreach($events_by_month->where(['month'=>'DEC') as $month=>$events)
{{ $month }}
@foreach($events as $event)
{{ $event }}
@endforeach
@endforeach
其中()
需要两个参数。看:@Blake,明白了。我的问题是,如果集合分组后需要两个参数,那么如何对其进行筛选?与您希望循环通过特定键的任何其他数组的方式相同foreach($array['key']as…)
@greener这样做@foreach($mth['DEC']as$mth=>$events)
@pratekkathal有很多个月的时间来满足需求
@foreach($events_by_month->where(['month'=>'DEC') as $month=>$events)
{{ $month }}
@foreach($events as $event)
{{ $event }}
@endforeach
@endforeach