显示通知逻辑
Displaying Notifications Logic
我正在使用 Laravel 框架构建一个社交网络,并且我有一个通知系统。
基本上每当用户以这 5 种不同的方式与网站互动时:
- 用户点赞 Post。
- Post.
中的用户评论
- 用户点赞评论。
- 用户回复评论。
- 用户点赞了一条回复。
通知被添加到该用户的通知table中(OP(原始poster)例如,如果用户赞成评论,则会为拥有的用户添加通知该评论,并在该通知中 table 我有这些配置。
$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 的 post 之一,这意味着如果我遍历未读通知数组,我将收到 100 个通知,但我想要这样的东西 'last user who interacted with the post' 。 'x' 人们有。 'interacted' 和你的 post.'
但我就是无法执行它,到目前为止,我已经设法计算出用户将使用以下逻辑看到的通知数量:
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();
我还没有测试过这个,但试一试 ;)
我正在使用 Laravel 框架构建一个社交网络,并且我有一个通知系统。
基本上每当用户以这 5 种不同的方式与网站互动时:
- 用户点赞 Post。
- Post. 中的用户评论
- 用户点赞评论。
- 用户回复评论。
- 用户点赞了一条回复。
通知被添加到该用户的通知table中(OP(原始poster)例如,如果用户赞成评论,则会为拥有的用户添加通知该评论,并在该通知中 table 我有这些配置。
$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 的 post 之一,这意味着如果我遍历未读通知数组,我将收到 100 个通知,但我想要这样的东西 'last user who interacted with the post' 。 'x' 人们有。 'interacted' 和你的 post.'
但我就是无法执行它,到目前为止,我已经设法计算出用户将使用以下逻辑看到的通知数量:
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();
我还没有测试过这个,但试一试 ;)