I know that there are many ways to mark as read all notifications of an User in L5.3 like this:
$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.
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.
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:
$('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 :
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?
If we want to mark notifications as read, we simple need a link and a route. Then create that route in the router; Route::get('/markAsRead', function(){auth()->user()->unreadNotifications->markAsRead();return redirect()->back();}); This will mark all notifications as read and redirect back to the same page.
Laravel Notify is a package that lets you add custom notifications to your project. A diverse range of notification design is available.
Slack Notification ChannelIn the older version of Laravel, Slack notification was built-in configured with the framework. But on this 5.8. x version, they have created a separate package for Slack Notification. First of all, we have to install that package in our application.
According to this Answer on Github, solution is :
Illuminate\Notifications\DatabaseNotificationis 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.
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
$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()])
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With