【发布时间】:2017-01-14 19:58:24
【问题描述】:
我正在使用 Laravel 框架构建一个社交网络,并且我有一个通知系统。
基本上每当用户以以下 5 种不同方式与网站交互时:
- 用户为帖子点赞。
- 帖子中的用户 cmets。
- 用户点赞评论。
- 用户回复评论。
- 用户为回复点赞。
一个通知被添加到该用户的通知表中(例如,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