Reputation: 9
i want to make the notifications mark as read when i checked , itry to put them as read but it not work here is my app.blade.php
<li class="dropdown" id="markasread" onclick="markNaotificationAsAread()">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="true">
<span class="glyphicon glyphicon-globe"></span> Notifications <span class="badge ">{{count(Auth()->user()->unreadNotifications)}}</span>
</a>
<ul class="dropdown-menu" role="menu">
@foreach(Auth()->user()->unreadNotifications as $notification)
<li>
@if(isset($notification->data['ad_object']))
<a href="#">{{$notification->data['user_name']}} posted a new ad "{{$notification->data['ad_object']}}"</a>
@endif
@if(isset($notification->data['comment_content']))
<a href="#">{{$notification->data['user_name']}} posted a new comment "{{$notification->data['comment_content']}}"</a>
@endif
</li>
@endforeach
</ul>
</li>
<li class="dropdown">
and here is my web.php
Route::get('/markAsRead',function(){
Auth()->user()->unreadNotifications->markAsRead();
});
what can i do please help me
Upvotes: 0
Views: 1651
Reputation: 552
Your code leaves much too the imagination in regards to how you're trying to mark the notifications as read but the simplest way would be to have a column in your notifications table 'read' data type Boolean/ tiny int.
When a notification is created the read columns default would be false.
Then when a notification is clicked on you can route the user to a simple method that updates the read columns in your notifications table to true. Then redirect the user to wherever the notification takes them.
Route::get('/markAsRead/{notification}',function($notification ){
$mark = Auth()->user()->unreadNotifications->where
('id', $notification)->first ();
$mark->update (['read' => true]);
return redirect ()->route('routename');
});
You can use something like that for a single notification when a user clicks on it. Alternatively if you wanted to just mark all notifications as read for some reason you would pretty much do the same thing whilst iterating tgrough the results.
You should move all that logic to a controller though.
Upvotes: 1