简体   繁体   English

Laravel 5.0:通知系统的实现(Facebook,Twitter和stackoverflow)

[英]Laravel 5.0: Implementation of Notification System(Facebook, Twitter and stackoverflow)

I am an absolute beginner of Laravel and web development. 我绝对是Laravel和Web开发的初学者。 I would like add a notification system similar to one on Facebook, Twitter and stackoveflow. 我想添加一个类似于Facebook,Twitter和stackoveflow上的通知系统。 However, I do not have any idea where to start. 但是,我不知道从哪里开始。 I would like to ask a few questions to anyone who is familiar about how to set the system. 我想向熟悉如何设置系统的任何人问几个问题。

1st question: 第一个问题:

I would like to know a method/function that tells me how many times a method/function is called. 我想知道一个方法/函数,它告诉我方法/函数被调用了多少次。 In my case, I would like to have the number of the function 'store' being called in the header.blade.php file. 就我而言,我想在header.blade.php文件中调用函数“ store”的编号。

Just like facebook, twitter, and stackoverflow tell you that your friends and followers put comments on your photo or tweet or that someone put answers on your question, and then the number shows up on the notification icon, I would like to the number of the function 'store' that has been called for a certain period of time. 就像facebook,twitter和stackoverflow告诉您,您的朋友和关注者在您的照片或tweet上发表评论,或者有人在您的问题上回答,然后数字显示在通知图标上,我想已被调用一定时间的函数“存储”。

ReasonsController.php ReasonsController.php

public function store(CreateReasonRequest $request, $course_id){

    $reason = new Reason($request->all());
    $reason->course_id = $course_id;
    \Auth::user()->reasons()->save($reason);
    return redirect('student/home');
}

Header.blade.php Header.blade.php

<a id="dLabel" role="button" data-toggle="dropdown" data-target="#" href="/page.html">
     <span class = "badge" id = "number">
         <i class="glyphicon glyphicon-bell">
            {{-- the number of the function 'store' being called comes here--}}
          </i>
     </span>
 </a>

2nd question: 第二个问题:

When the user clicks the icon on facebook and twitter, the number on the icon disappears as you can imagine. 当用户单击Facebook和Twitter上的图标时,图标上的数字就会消失,这是您可以想象的。 How am I supposed to do that? 我应该怎么做? What language should I use, javascript? 我应该使用哪种语言,javascript? jquery? jQuery的? css? CSS?

English is not my first language, so please leave your comments if this post does not make sense. 英语不是我的母语,所以如果这篇文章没有意义,请留下您的评论。 ANY ADVICE WOULD BE APPRECIATED!! 任何意见,将不胜感激!! THANKS IN ADVANCE! 提前致谢!

1st question: 第一个问题:

Every time store() is called, you save a new Reason to database and associate it with the user right? 每次调用store() ,都将新的Reason保存到数据库并将其与用户权限相关联吗? To show the number of reasons, you just have to figure out how many records (in the table reasons ) there are associated with the logged in user. 要显示原因数量,您只需要找出与登录用户相关联的记录数(在表reasons )。 Judging from the code above, you've already set up the reasons relationship in your User model, getting the total number of rows is as easy as calling \\Auth::user()->reasons()->count(); 从上面的代码来看,您已经在用户模型中设置了reasons关系,获取总行数就像调用\\Auth::user()->reasons()->count(); .

Put this code in your Header.blade.php 将此代码放在Header.blade.php中

   @if (Auth::guest())
      Please login first.
   @else
     {{ Auth::user()->reasons()->count() }}
   @endif

2nd question: 第二个问题:

Suppose you have a notifications table, you may add a unread boolean to the schema to know whether or not the user has read the notification. 假设您有一个notifications表,则可以向架构中添加一个unread布尔值,以了解用户是否已阅读该通知。 When you need the count of unread notifications, you write 当您需要计数未读通知时,您可以编写

Auth::user()->notifications()->where("unread",true)->count()

When you click on the icon, an AJAX request is sent to the server to fetch all the unread messages. 当您单击该图标时, AJAX request将发送到服务器以获取所有未读消息。 JavaScript is used to clear the notification count. JavaScript用于清除通知计数。 And at the same time, the server sets the unread attributes of all notifications related to that user to false. 同时,服务器将与该用户相关的所有通知的unread属性设置为false。 So that upon page refresh, Auth::user()->notifications()->where("unread",true)->count() will return 0. 这样,在刷新页面时, Auth::user()->notifications()->where("unread",true)->count()将返回0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM