Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

$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?

like image 646
A.B.Developer Avatar asked Sep 05 '16 11:09

A.B.Developer


People also ask

How do I get laravel to read a notification?

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.

What is notify () in laravel?

Laravel Notify is a package that lets you add custom notifications to your project. A diverse range of notification design is available.

What is slack notification in laravel?

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.


2 Answers

According to this Answer on Github, solution is :

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.

like image 125
A.B.Developer Avatar answered Sep 22 '22 07:09

A.B.Developer


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()])
like image 37
Sfwan Essam Avatar answered Sep 21 '22 07:09

Sfwan Essam