【问题标题】:Mark as read after accept friend's request接受好友请求后标记为已读
【发布时间】: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


【解决方案1】:

您将需要与 LIKE 查询等效的查询生成器。

->where('data', '%12%') 将生成 SQL:data = '%12%' 不返回任何结果。

->where('data', 'like', '%12%') 将生成 SQL:data like '%12%',这应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多