【问题标题】:Displaying Notifications Logic显示通知逻辑
【发布时间】:2017-01-14 19:58:24
【问题描述】:

我正在使用 Laravel 框架构建一个社交网络,并且我有一个通知系统。

基本上每当用户以以下 5 种不同方式与网站交互时:

  1. 用户为帖子点赞。
  2. 帖子中的用户 cmets。
  3. 用户点赞评论。
  4. 用户回复评论。
  5. 用户为回复点赞。

一个通知被添加到该用户的通知表中(例如,OP(原始发帖人),如果用户支持评论,则会为拥有该评论的用户添加一个通知,并且在该通知表中我有这些配置。

$table->string('id')->primary();
        $table->string('type');
        $table->morphs('notifiable');
        $table->text('data');
        $table->timestamp('read_at')->nullable();
        $table->timestamps();

因此,基本上我可以使用命令Auth::user()->unreadNotifications 获取每个已登录用户的未读通知,该命令是一个包含所有通知信息的数组。

但这是我的问题。例如,有 100 个用户对 OP 的一个帖子投了赞成票,这意味着如果我循环遍历未读通知数组,我将收到 100 个通知,但我想要这样的东西,'last user who interacted with the post'。 'x' 人有 .与您的帖子“互动”。'

但我只是无法执行它,到目前为止,我已经设法使用此逻辑计算用户将看到的通知数量:

public function get_Notification_Count()
{
    $total_notifications = Auth::user()->unreadNotifications;
    $num_of_posts = array();
    $num_of_comments = array();
    $num_of_replies = array();
    $num_of_upvoted_comments = array();
    $num_of_upvoted_replies = array();
    // $num_of_notifications = array();

    foreach ($total_notifications as $notification) {
        if ($notification->type == 'App\Notifications\UserUpvotedPost') {
            array_push($num_of_posts, $notification->data['post_id']);
            $num_of_posts = array_unique($num_of_posts);
        }
        if ($notification->type == 'App\Notifications\UserCommented') {
            array_push($num_of_comments, $notification->data['post_id']);
            $num_of_comments = array_unique($num_of_comments);
        }
        if ($notification->type == 'App\Notifications\UserReplied') {
            array_push($num_of_replies, $notification->data['comment_id']);
            $num_of_replies = array_unique($num_of_replies);
        }
        if ($notification->type == 'App\Notifications\UserUpvotedComment') {
            array_push($num_of_upvoted_comments, $notification->data['comment_id']);
            $num_of_upvoted_comments = array_unique($num_of_upvoted_comments);
        }
        if ($notification->type == 'App\Notifications\UserUpvotedReply') {
            array_push($num_of_upvoted_replies, $notification->data['reply_id']);
            $num_of_upvoted_replies = array_unique($num_of_upvoted_replies);
        }
    }

    $num_of_notifications = count(array_merge($num_of_posts, $num_of_comments, $num_of_replies, $num_of_upvoted_comments, $num_of_upvoted_replies));

    return $num_of_notifications;
}

【问题讨论】:

    标签: php laravel notifications social-networking


    【解决方案1】:

    我认为最好的办法是将查询分组:

    Auth::user()->unreadNotifications()->select(\DB::raw('COUNT(*) as count'), 'type')
    ->groupBy('type')
    ->get();
    

    我还没有测试过这个,但是试一试;)

    【讨论】:

    • 你的答案很漂亮,我从没想过使用查询构建器来解决我的问题,我还有一个问题,如何获取数据,就像我尝试过的类型一样 return Auth::user()->unreadNotifications()->select(DB::raw('COUNT(*) as count'), 'type', 'data') ->groupBy('type') ->get(); 我得到了一个错误。
    • 您的回答也有问题,想象一下用户对 OP 的两个不同帖子点赞,由于按类型分组,我仍然会收到 1 条通知。
    • 您遇到什么错误?对于你的第二个问题:->groupBy('type', 'other_user_id') other_user_id 应该在哪里替换
    • 另外,如果您将数据列设为 JSON 列,我相信您也可以对其进行分组
    • 这是来自notifications 的错误SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel53.notifications.data' isn't in GROUP BY (SQL: select data, type`,其中notifications.notifiable_id = 1 和notifications.notifiable_id 不为空且notifications .notifiable_type = App\User and read_at is null group by type order by created_at desc)` 是的,它是一个 JSON 列,到目前为止我没有运气。
    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2023-02-09
    • 2023-04-07
    • 2019-09-21
    • 2021-05-28
    相关资源
    最近更新 更多