为了账号安全,请及时绑定邮箱和手机立即绑定

where year 过滤 Laravel 集合

where year 过滤 Laravel 集合

PHP
阿晨1998 2021-06-10 14:20:00
我有一个带有“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"      ]
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您可以使用 Collections 方法过滤器来仅查找年份符合您条件的那些:


$collection = Model::all();


$collection->filter(function ($value) {

    return $value->created_at->year === 2019; // assuming, that your timestamp gets converted to a Carbon object.

});


查看完整回答
反对 回复 2021-06-19
  • 1 回答
  • 0 关注
  • 194 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信