where year 过滤 Laravel 集合

我有一个带有“created_at”属性的 Laravel 集合,我想按年份过滤,就像在 eloquent 中一样:


$model->whereYear()

但是在一个集合中。


谢谢!


#attributes: array:10 [

        "id" => 720

        "created_at" => "2019-05-15 08:24:00"

        "updated_at" => "2019-05-15 08:24:00"

      ]


阿晨1998
浏览 195回答 1
1回答

慕桂英3389331

您可以使用 Collections 方法过滤器来仅查找年份符合您条件的那些:$collection = Model::all();$collection->filter(function ($value) {    return $value->created_at->year === 2019; // assuming, that your timestamp gets converted to a Carbon object.});
打开App,查看更多内容
随时随地看视频慕课网APP