我正在使用 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 个用户赞成一个 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;
}
最佳答案
我认为您最好的选择是对查询进行分组:
Auth::user()->unreadNotifications()->select(\DB::raw('COUNT(*) as count'), 'type')
->groupBy('type')
->get();
我还没有测试过这个,但试一试 ;)
关于php - 显示通知逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364607/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]