【发布时间】:2019-08-12 20:40:01
【问题描述】:
我有一个简单的函数来接受朋友的请求:
public function acceptFriend($id){
$user = User::find($id);
$sender = Auth::user();
$sender->acceptFriendRequest($user);
return redirect()->back();
}
它工作正常,但如果其他用户发送朋友的请求,他会发送通知。我想在接受请求后将其标记为已读,但我遇到了问题。我不知道如何进行查询构建以检查此通知。我知道我可以创建一个新功能,并且喜欢使用通知 ID,但我网站的用户不仅可以在通知列表中接受请求,还可以在用户个人资料中接受请求。
我想过从“通知”表中读取“数据”列中的信息,但我遇到了问题。我尝试了一些查询建筑:
$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
}
}
【问题讨论】:
-
通知表的结构是什么样的?
-
正常通知结构:
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}}
标签: php mysql laravel notifications