![](/img/trans.png)
[英]Laravel 4: How to apply a WHERE condition to all queries of an Eloquent class?
[英]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.