php - Laravel 集合 : GroupBy items and use WhereIn in the view

标签 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 => {#166 ▼
          // attributes
        }
      ]
    },
     "JAN" => Collection {#159 ▼
      #items: array:1 [▼
        0 => {#166 ▼
          // attributes
        }
      ]
    }
  ]
}

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

whereIn 中没有键返回错误:

@foreach($mths->whereIn(['DEC', 'JAN']) as $mth => $events)

最佳答案

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

在 View 中:

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

       @foreach($events as $event)
         {{ $event }} 
       @endforeach

   @endforeach

关于php - Laravel 集合 : GroupBy items and use WhereIn in the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880706/

相关文章:

php - 将 URL ID 更改为简单文本

javascript - 如何清理 JavaScript 中使用的参数?

php - 为什么我的站点在 xampp 中运行但不在我的 Apache 服务器中运行,而我在 Ubuntu 上设置的 mySQL?

php - 在 Laravel 5.4 的浏览器中预览 Mailables 时出错

php - 如果可选参数不在列表中,Laravel 处理默认路由

php - Laravel 4.2 动态数据库连接

php - 在 echo PHP 之前重新格式化日期

php - 我如何正确测试 Laravel 事件?

mysql - 在 Laravel 应用程序中检索超过 10,000 行

javascript - 制作多行三个相关的保管箱