【问题标题】:How to fetch unread notifications first and then read notifications in laravel如何先获取未读通知,然后在 laravel 中读取通知
【发布时间】:2021-04-29 16:08:11
【问题描述】:

我想从数据库中获取用户的所有通知。但我想先用未读通知过滤它们,然后再阅读通知。 比方说,我有 3 个未读通知给一个用户,其中有一个已读通知。然后我将获取所有通知,其中未读 3 个通知将首先出现在集合中,然后是已读通知。 我没有看到任何雄辩的方式来做到这一点。 提前致谢。

【问题讨论】:

    标签: laravel eloquent laravel-8


    【解决方案1】:

    要首先获取所有未读通知,您需要在read_at 上应用orderBy

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多