我是Laravel和Web开发的绝对初学者。我想添加一个类似于Facebook,Twitter和stackoveflow上的通知系统。但是,我不知道从哪里开始。我想向任何熟悉如何设置系统的人提出几个问题。
第一个问题:
我想知道一个方法/函数,它告诉我一个方法/函数被调用了多少次。就我而言,我希望在 header.blade.php 文件中调用函数"store"的编号。
就像Facebook,Twitter和stackoverflow告诉您您的朋友和关注者对您的照片或推文发表评论,或者有人对您的问题进行了回答,然后数字显示在通知图标上,我想要已经调用了一段时间的功能"store"的编号。
原因控制器.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');
}
标题刀片.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>
第二个问题:
当用户点击Facebook和Twitter上的图标时,图标上的数字会消失,正如您可以想象的那样。我应该怎么做?我应该使用什么语言,javascript?jquery?.css?
英语不是我的第一语言,所以如果这篇文章没有意义,请留下您的评论。任何建议将不胜感激!!提前感谢!
第一个问题:
每次调用store()
时,您都会将新Reason
保存到数据库并将其与用户关联,对吗?要显示原因的数量,您只需确定有多少条记录(在表中reasons
)与登录用户相关联。从上面的代码来看,您已经在 User 模型中设置了reasons
关系,获取总行数就像调用 'Auth::user()->reasons()->count();
一样简单。
将此代码放在 Header.blade 中.php
@if (Auth::guest())
Please login first.
@else
{{ Auth::user()->reasons()->count() }}
@endif
第二个问题:
假设您有一个notifications
表,则可以向架构添加unread
布尔值,以了解用户是否已阅读通知。当您需要未读通知的计数时,您可以编写
Auth::user()->notifications()->where("unread",true)->count()
当您单击该图标时,会将AJAX request
发送到服务器以获取所有未读消息。JavaScript 用于清除通知计数。同时,服务器将与该用户相关的所有通知的unread
属性设置为 false。这样在页面刷新时,Auth::user()->notifications()->where("unread",true)->count()
将返回 0。