Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示通知逻辑_Php_Laravel_Notifications_Social Networking - Fatal编程技术网

Php 显示通知逻辑

Php 显示通知逻辑,php,laravel,notifications,social-networking,Php,Laravel,Notifications,Social Networking,我正在用Laravel框架建立一个社交网络,我有一个通知系统 基本上,当用户以以下5种不同方式与网站交互时: 用户上传帖子 用户在帖子中的评论 用户对评论进行投票 用户回复评论 用户需要回复 通知被添加到该用户的通知表(OP(原始海报)中,例如,如果用户对评论进行投票,则会为拥有该评论的用户添加通知,在该通知表中,我有这些配置 $table->string('id')->primary(); $table->string('type'); $ta

我正在用Laravel框架建立一个社交网络,我有一个通知系统

基本上,当用户以以下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个用户向上投票一篇文章,这意味着如果我在未读通知数组中循环,我将得到100个通知,但我想要的是这样的东西,“最后一个与文章交互的用户”。“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;
    }
    

    我认为您最好将查询分组:

    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应该被替换的地方plus,如果您将数据列设置为JSON列,我相信您可以在该列上分组,这是错误
    SQLSTATE[42000]:语法错误或访问冲突:1055'laravel53.notifications.data'不在groupBy中(SQL:select
    data
    type`from
    notifications
    其中
    notifiable\u id
    =1和
    notifications
    notifiable\u id
    不为空且
    notifications
    notifiable\u type
    =App\User和
    read\u at
    为空y
    created_at
    desc)`是的,这是一个JSON列,我到目前为止运气不好。