frankwaive's avatar

Mark Notification as read

I want to mark a single notification as read.

$user->unreadNotifications->markAsRead();

marks all notifications as read, i only want to mark the clicked/selected notification as read. Any ideas how to get this done?

0 likes
6 replies
tisuchi's avatar

In your notification table, set a column hasread where by default value is 0.

Now think of it, if you call markAsRead() it will take notification id as hidden and change the default value of hadread from 0 to 1.

Finally in your unread notification display area, show & count unread when hasread = 0.

1 like
frankwaive's avatar

I am currently using the default notifications table so am pretty sure it has that covered. When I call $user->unreadNotifications->markAsRead(); it still marks all unread notifications as read, i want to mark a single one

tisuchi's avatar

That's what I said in my previous comment.

Now think of it, if you call markAsRead() it will take notification id as hidden and change the default value of hadread from 0 to 1.

Finally in your unread notification display area, show & count unread when hasread = 0.

1 like
frankwaive's avatar

Here's my code, maybe this would help

Somewhere in my header


@if(empty(count(auth()->user()->unreadNotifications)))

                    <li class="dropdown" id="markasunread">
                                                    <a>
                                                        <span class="badge fa fa-bell"> {{count(auth()->user()->unreadNotifications)}} </span>
                                                    </a>
                    </li>

                            
@else
                    <li class="dropdown" id="markasunread">
                                                    <a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"charset="" data-original-title="Profile and settings">
                                                        <span class="badge fa fa-bell"> {{count(auth()->user()->unreadNotifications)}} </span>
                                                    </a>


                                                    <ul class="dropdown-menu" role="menu">

                                                        <?php $user= Auth::user(); ?>
                                                            @foreach ($user->unreadNotifications as $notification) 

                                                        <li>
                                                            
                                                              @include('notification.'.snake_case(class_basename($notification->type))) 

                                                        </li>
                                                        
                                                        @endforeach
                                                    </ul>
                    </li>



@endif

Partial replied_to_post.blade.php

<a href="{{url('notifications/'.$notification->id)}}">{{$notification->data['user']['name']}} commented on your post.

</a>
tisuchi's avatar

I am just giving you a rough idea for that.

@foreach(Auth::user()->unreadNotifications as $notification) 
    <li>
        <input type="hidden" id="notificationid" value="{{ $notification->id }}" />
        {{ $notification->whocommented }} has commented on your post.
    </li>
@endforeach

Note: here you may need to adjust id and whocommented.

//call ajax for submitting this form

Now in your controller method-

public function delete(){
    //imagine that you have passed id from ajax form submission
    $id = request()->input('id')

    //assuming that, in notification table there is a column called hasread that is by default 0.
    if($hasNotification = Notification::find($id)){
        $hasNotification->hasread = 1;
        $hasNotification->save();
    }
    return back();
}
2 likes
frankwaive's avatar
frankwaive
OP
Best Answer
Level 1

Finally figured it out, changed

<a href="{{url('notifications/'.$notification->id)}}">{{$notification->data['user']['name']}} commented on your post.

</a>

to

             <form action="{{url('notification/' . $notification->id)}}" method="POST">
            {{ Form::open(array('action' => array('NotificationController@delete', $notification->id, 'method' => 'POST'))) }}
            {{ Form::hidden('id', $notification->id) }}
            {{ Form::hidden('slug', $notification->data['comment']['post']['slug']) }}

            {{ Form::submit(" commented on your post.", ['class' => 'btn']) }}
            
            {{ Form::close() }}

Then created a notification controller function to delete the notification and redirect to the post

Please or to participate in this conversation.