[英]Mark as read a specific notification in Laravel 5.3 Notifications
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
标签都有一个包含通知 ID 的data-notif-id
属性。
Now I want to send this ID to a script via Ajax (on click event) and mark as read that notification only.现在我想通过 Ajax(点击事件)将这个 ID 发送到脚本,并标记为只读该通知。 for that I wrote this:
为此我写了这个:
$('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
指的是波纹管 Controller:
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
Model 的情况下我该怎么做?
According to this Answer on 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.Illuminate\\Notifications\\DatabaseNotification
是通知模型所在的位置,您可以使用它通过 ID 抓取通知并将其删除。 Also if you don't want to use the model you can use a normal DB query.此外,如果您不想使用该模型,您可以使用普通的数据库查询。
This works without fail:这绝对有效:
$id = auth()->user()->unreadNotifications[0]->id;
auth()->user()->unreadNotifications->where('id', $id)->markAsRead();
$notification = auth()->user()->notifications()->find($notificationid);
if($notification) {
$notification->markAsRead();
}
You must first make sure that the notification exists first so that you can make adjustments to it and I do not recommend using the unreadNotifications object because in the event that there are no unread notifications an error will occur because of that您必须首先确保通知存在,以便您可以对其进行调整,我不建议使用 unreadNotifications 对象,因为如果没有未读通知,则会因此发生错误
$notification_id = "03fac369-2f41-43d0-bccb-e364aa645f8a";
$Notification = Auth::user()->Notifications->find($notification_id);
if($Notification){
$Notification->markAsRead();
}
or you can directly edit without any check或者您可以直接编辑而无需任何检查
DB::table('notifications')->where('id',$notification_id)->update(['read_at'=>Carbon::now()])
This method worked fine for me:这种方法对我很有效:
$unreadNotifications = Auth::user()->unreadNotifications()->whereIn('id', $items)->get();
where $items is an array of notifications' ids!其中 $items 是一组通知的 ID!
Instead of the following :而不是以下:
$user->unreadNotifications->markAsRead();
let's use :让我们使用:
$user->unreadNotifications->first()->markAsRead();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.