douti19680318 2019-08-12 20:40 采纳率: 0%
浏览 96
已采纳

接受朋友的请求后标记为已读

I have a simple function to accept a friend's request:

public function acceptFriend($id){
  $user = User::find($id);
  $sender = Auth::user();

  $sender->acceptFriendRequest($user);

  return redirect()->back();
}

It work's correctly but if other user send a friend's request, he send a notification. I want to mark as read it after accept request but I have a problem.I don't know how I can do query building to check this one notify. I know I can make a new function and like attirute use notify id but user of my website can accept request not only in notify list but in user's profile too.

I thought about read information form "data" column from "notifications" table but I have a problem. I try a few of query buildings:

$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
                             ->where('data->arr->id', '12')->first();

or

$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
                             ->where('data', '%12%')->first();

But it doesn;t works. "12" is id of sender,

The table structure is the normal notifications structure:

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();
}); 

The data column will contain an array such as:

{
   "title":"Something",
   "arr":{
      "id":12,
      "name":"HelloWorld",
      "avatar":null
   }
}
  • 写回答

1条回答 默认 最新

  • doubo4824 2019-08-13 15:23
    关注

    You will need the Query Builder equivalent of a LIKE query.

    ->where('data', '%12%') will generate SQL: data = '%12%' returning no results.

    ->where('data', 'like', '%12%') will generate SQL: data like '%12%' which should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站