如何先获取未读通知,然后在laravel中读取通知

如何先获取未读通知,然后在laravel中读取通知,laravel,eloquent,laravel-8,Laravel,Eloquent,Laravel 8,我想从数据库中获取用户的所有通知。但我想先获取经过未读通知过滤的通知,然后读取通知。 比方说,我有3个未读通知,一个用户有一个已读通知。然后我将获取所有通知,其中未读的3个通知将首先出现在集合中,然后是已读的通知。 我看不出有任何雄辩的方式来做这件事。 提前感谢。通过以下简单的集合: $res = [ 'unread' => auth()->user()->unreadnotifications 'read' => auth

我想从数据库中获取用户的所有通知。但我想先获取经过未读通知过滤的通知,然后读取通知。 比方说,我有3个未读通知,一个用户有一个已读通知。然后我将获取所有通知,其中未读的3个通知将首先出现在集合中,然后是已读的通知。 我看不出有任何雄辩的方式来做这件事。
提前感谢。

通过以下简单的集合:

$res = [
            'unread' => auth()->user()->unreadnotifications
            'read' => auth()->user()->notifications()->whereNotNull('read_at')->get(),
        ];

通过这样一个简单的集合数组:

$res = [
            'unread' => auth()->user()->unreadnotifications
            'read' => auth()->user()->notifications()->whereNotNull('read_at')->get(),
        ];

要首先获取未读的所有通知,您需要在
read\u上应用
orderBy

$notifications = auth()->user()->notifications()
                               ->orderBy('read_at', 'asc')
                               ->orderBy('created_at', 'desc')
                               ->get()

要首先获取未读的所有通知,您需要在
read\u上应用
orderBy

$notifications = auth()->user()->notifications()
                               ->orderBy('read_at', 'asc')
                               ->orderBy('created_at', 'desc')
                               ->get()