繁体   English   中英

如何在Laravel中将收集条件应用于何处?

[英]How to apply where condition on collection result in Laravel?

我想获得的所有通知,然后在特定的情况下适用where该结果的条件。

$allNotifications = $user->notifications()->unread()->orderBy('created_at', 'desc')->get();

然后我会检查是否发生了一些条件,使应用另一个where上面收集条件。

$notifications = $allNotifications->where('created_at', '>', $timestamps);

这意味着从$allNotifications获取与已定义条件匹配的那些通知。

使用filter()方法:

$notifications = $allNotifications->filter(function ($item) use ($timestamp) {
    return $item->created_at > $timestamp;
});

$notifications->all();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM