我有一个简单的函数来接受朋友的请求:
public function acceptFriend($id){
$user = User::find($id);
$sender = Auth::user();
$sender->acceptFriendRequest($user);
return redirect()->back();
}它工作正常,但如果其他用户发送朋友请求,他会发送通知。我想在接受请求后将其标记为已读,但我有一个问题。我不知道如何进行查询构建来检查此通知。我知道我可以做一个新的功能,像attirute一样使用notify id,但是我的网站的用户不仅可以在notify列表中接受请求,还可以在用户的个人资料中接受请求。
我想过从"notifications“表的"data”列中读取信息,但我遇到了一个问题。我尝试了几个查询构建:
$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
->where('data->arr->id', '12')->first();或
$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
->where('data', '%12%')->first();但它不起作用。"12“是发送者的id,
表结构是正常的通知结构:
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
}); 数据列将包含一个数组,例如:
{
"title":"Something",
"arr":{
"id":12,
"name":"HelloWorld",
"avatar":null
}
}发布于 2019-08-13 23:23:51
您将需要与LIKE查询等效的查询生成器。
->where('data', '%12%')将生成SQL:data = '%12%',不返回任何结果。
->where('data', 'like', '%12%')将生成SQL:data like '%12%',这应该可以正常工作。
https://stackoverflow.com/questions/57467964
复制相似问题