我知道有很多方法可以像这样在 L5.3 中标记为已读用户的所有通知:
$user = App\User::find(1);
foreach ($user->unreadNotifications as $notification) {
$notification->markAsRead();
}
或者:
$user->unreadNotifications->markAsRead();
但假设我想将具有给定 ID 的特定通知标记为已读。
其实我已经做了一个这样的未读用户通知列表:
<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>
如您所见,每个 a
标签都有一个包含通知 ID 的 data-notif-id
属性。
现在我想通过 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
指的是下面的 Controller :
class NotificationController extends Controller
{
public function MarkAsRead (Request $request)
{
//What Do I do Here........
}
}
在没有任何Notification
模型的情况下我该怎么做?
最佳答案
根据 this Answer在 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.
关于php - 在 Laravel 5.3 通知中将特定通知标记为已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39329414/