如何在Laravel 5中将特定用户和帖子的通知标记为已读?

3
我想为查看特定帖子的用户标记通知为已读。
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'));
    }

它会出现这个错误。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'data['post_id']' in 'where clause' 

当该通知显示时,我如何访问数据列的post_id以便为用户删除特定通知。

以下是数据列中存储的方式

{"post_id":8,"title":"Example Post...}

$notification_for_user = auth()->user()->unreadNotifications()->whereRaw("data.post_id", $post->id)->first()->update(['read_at' => now()]); - Sohel0415
仍然出现相同的错误: SQLSTATE[42S22]: 列未找到: 1054 在'where子句'中找不到'data.post_i‌​d'列。 - Raj
1
展示一下你的unreadNotifications()函数,如果你能更新一下你的问题并附上数据库模式图就更好了。 - Sohel0415
也许这些帖子可以帮到你。看一下:1. Laravel 5.3中新的JSON列where()和update()语法 2. 使用生成的列更快地查找Laravel JSON文档 - Parantap Parashar
看起来这些列是json格式的。请参考https://laravel.com/docs/5.7/queries#json-where-clauses - Clément Baconnier
显示剩余2条评论
2个回答

2
请检查以下代码。
use Illuminate\Notifications\DatabaseNotification;

    $notification = DatabaseNotification::find( $request->notification_id );
                $notification->update(['read_at' => now()]);
                $notification->save();

0
在web.php文件中: Route::get('/read/notifications', 'Admin\NotificationController@readNotification'); 在控制器中:
public function readNotification() {
$user = Auth::user();
$user->unreadNotifications()->update(['read_at' => now()]);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接