Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel集合:按项目分组并在视图中使用where_Php_Laravel - Fatal编程技术网

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