Php Laravel数据库通知-按帖子id和类型分组

Php Laravel数据库通知-按帖子id和类型分组,php,mysql,laravel,Php,Mysql,Laravel,我无法找出Laravel中数据库通知的一个问题。 我希望显示像Facebook这样的用户通知。例如:“用户X、Y和另外两个人评论了你的照片”。我的数据库结构如下所示: public function getNotificationsAttribute() { return $this ->notifications() ->get() ->groupBy("type"), ->groupBy(func

我无法找出Laravel中数据库通知的一个问题。 我希望显示像Facebook这样的用户通知。例如:“用户X、Y和另外两个人评论了你的照片”。我的数据库结构如下所示:

public function getNotificationsAttribute()
{
    return $this
        ->notifications()
        ->get()
        ->groupBy("type"),
        ->groupBy(function($data) {
            return $data["albumId"];
        })
        ->orderBy("created_at", "desc");
}
+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+
|id |类型|应报告|类型|应报告| id |数据|读取|创建|更新||
+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+
|6d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User | 1 |{“userId”:4,“commentId”:42,“albumId”:3}| | 2020-01-29 18:00:10 | 2020-01-29 18:03:15|
|7d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User | 1 |{“userId”:4,“commentId”:42,“albumId”:3}| 2020-01-29 18:00:15 | 2020-01-29 18:03:15|
|8d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User | 1 |{“userId”:5,“commentId”:42,“albumId”:2}| | 2020-01-29 18:03:15 | 2020-01-29 18:03:15|
|9d57d30c-419c-4f22-9aaa-f6bb22354887 | App\Notifications\NewComment | App\Models\User | 1 |{“userId”:4,“commentId”:42,“albumId”:2}| | 2020-01-29 18:02:15 | 2020-01-29 18:03:15|
+--------------------------------------+------------------------------+-----------------+---------------+-----------------------------------------+---------+---------------------+---------------------+
我不知道如何按
类型
相册ID
(在
数据
列中)分组。您能帮我吗?我仍在覆盖用户模型方法:

公共函数getNotificationsAttribute()
{
$notifications=$this->notifications();
$notifications->groupBy('type','data->albumId')->orderBy('created_at','desc')->get();
返回$通知;
}

使用查询生成器执行此操作非常具有挑战性(因为这不是JSON列,所以更具挑战性),但如果您使用的是集合,则执行以下操作非常简单:

public function getNotificationsAttribute()
{
    return $this
        ->notifications()
        ->get()
        ->groupBy("type"),
        ->groupBy(function($data) {
            return $data["albumId"];
        })
        ->orderBy("created_at", "desc");
}
这假设雄辩的模型配置为将列强制转换为数组

protected $casts = ["data" => "array"];

您的
数据
字段的类型是json吗?为什么有这么多双引号?谢谢你的回复,数据字段是文本类型。使用artisan(
php artisan notifications:table
)编辑创建通知表时,它是默认的数据类型-双引号可能是我的错误。当我导出表时,我选择TSV格式,它添加了双引号。很抱歉