Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark notification as read for particular user and post in laravel 5?

I want to mark a notification as read for a user who views a particular post

public function show(Post $post)
    {

        $notification_for_user = auth()->user()->unreadNotifications()->where("data['post_id']", $post->id)->first()->update(['read_at' => now()]);

        return view('post.show', compact('post'));
    }

It gives this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'data['post_id']' in 'where clause' 

So how can i access data column's post_id to delete that particular notification for the user when it is displayed.

This is how it is stored in data column

{"post_id":8,"title":"Example Post...}
like image 644
Raj Avatar asked Nov 28 '25 18:11

Raj


1 Answers

Please check the following code.

use Illuminate\Notifications\DatabaseNotification;

    $notification = DatabaseNotification::find( $request->notification_id );
                $notification->update(['read_at' => now()]);
                $notification->save();
like image 101
Arshid KV Avatar answered Nov 30 '25 08:11

Arshid KV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!