在 Laravel 5.3 通知中将特定通知标记为已读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39329414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mark as read a specific notification in Laravel 5.3 Notifications
提问by A.B.Developer
I know that there are many ways to mark as read all notifications of an User in L5.3 like this:
我知道有很多方法可以将 L5.3 中用户的所有通知标记为已读,如下所示:
$user = App\User::find(1);
foreach ($user->unreadNotifications as $notification) {
$notification->markAsRead();
}
Or:
或者:
$user->unreadNotifications->markAsRead();
But suppose I want to mark as read a specific notification with a given ID.
但是假设我想用给定的 ID 将特定通知标记为已读。
In fact, I have made a list of unread user notifications like this:
事实上,我已经列出了这样的未读用户通知:
<ul class="menu">
@foreach($AuthUser->unreadNotifications as $notif)
<li>
<a href="{{$notif->data['action']}}" data-notif-id="{{$notif->id}}">
{{$notif->data['message']}}
</a>
</li>
@endforeach
</ul>
As you can see, each a
tag has a data-notif-id
attribute contain notif ID.
如您所见,每个a
标签都有一个data-notif-id
包含 notif ID的属性。
Now I want to send this ID to a script via Ajax (on click event) and mark as read that notification only. for that I wrote this:
现在我想通过 Ajax(点击事件)将此 ID 发送到脚本,并将该通知标记为只读。为此我写了这个:
$('a[data-notif-id]').click(function () {
var notif_id = $(this).data('notifId');
var targetHref = $(this).data('href');
$.post('/NotifMarkAsRead', {'notif_id': notif_id}, function (data) {
data.success ? (window.location.href = targetHref) : false;
}, 'json');
return false;
});
NotifMarkAsRead
refers to bellow Controller :
NotifMarkAsRead
指的是波纹管控制器:
class NotificationController extends Controller
{
public function MarkAsRead (Request $request)
{
//What Do I do Here........
}
}
How can I do that while there is not any Notification
Model?
当没有任何Notification
模型时我该怎么做?
采纳答案by A.B.Developer
According to this Answeron Github, solution is :
根据Github上的这个答案,解决方案是:
Illuminate\Notifications\DatabaseNotification
is where the Model for the notifications exists, you can use it to grab a notification by ID and delete it. Also if you don't want to use the model you can use a normal DB query.
Illuminate\Notifications\DatabaseNotification
是通知模型所在的位置,您可以使用它通过 ID 获取通知并将其删除。此外,如果您不想使用该模型,您可以使用普通的数据库查询。
回答by Sandesh Satyal
This works without fail:
这绝对有效:
$id = auth()->user()->unreadNotifications[0]->id;
auth()->user()->unreadNotifications->where('id', $id)->markAsRead();
回答by Mario
$notification = auth()->user()->notifications()->find($notificationid);
if($notification) {
$notification->markAsRead();
}
回答by Sfwan Essam
you need first to check if unreadNotifications its not empty because if you make query and the unreadNotifications object was empty that is the problem .
您首先需要检查 unreadNotifications 是否不为空,因为如果您进行查询并且 unreadNotifications 对象为空,那就是问题所在。
$notification_id = "03fac369-2f41-43d0-bccb-e364aa645f8a";
if (auth()->user()->unreadNotifications->count() > 0) {
auth()->user()->unreadNotifications->where('id', $notificationid)->first()->markAsRead();
}
回答by Anonymous
Instead of the following :
而不是以下:
$user->unreadNotifications->markAsRead();
let's use :
让我们使用:
$user->unreadNotifications->first()->markAsRead();