首页 > 解决方案 > Laravel:如何根据通知数组中存在的数据删除数据库通知

问题描述

当买家用户向卖家发送问题时,我已按照链接在我的 laravel 电子商务应用程序中创建数据库通知

我的应用程序成功创建数据库通知如下

表:通知

id | type | notifiable_id | notifiable_type | data | read_at | created_at | updated_at
id = 0b2a7fdf-eea4-4982-a86d-e874bb4f28ef

type = App\Notifications\BuyerQuestionNotification

notifiable_id= 48 

data = {"message":"Someone asked question for Hostelfe","url":"#", "question_id":12024}

read_at = NULL

created_at = 2018-04-07 12:46:42

updated_at = 2018-04-07 12:46:42

现在我想通过 data->question_id 删除数据库通知行

I have tried below query :

DB::table('notifications')
    ->where('type','App\Notifications\SellerAnswerNotification')
    ->where('data->question_id',2079107489)
    ->first();

得到错误:

[2019-03-31 13:22:03] local.ERROR: SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在 '>'$."question_id"' = ?' 附近使用的正确语法 在第 1 行 (SQL: select * from notificationswhere type= App\Notifications\SellerAnswerNotification and data->'$."question_id"' = 2079107489)

我也尝试过以下查询:

$result=DB::table('notifications')->whereRaw("JSON_EXTRACT(data, '$.question_id') = ?", [2079107489]);

但得到了类似的错误

如何更正查询以在通知表中按 json 获取行和删除行?

标签: laravelnotifications

解决方案


给你,我试过这个,它奏效了:

DB::table('notifications')
->where('type','App\Notifications\BuyerQuestionNotification')
->where('data->question_id',12024)
->first();

注意:它仅适用于 MySQL 5.7.13 及更高版本。进一步阅读: https ://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#operator_json-inline-path https://laravel.com/docs/5.8/queries#json -where子句


推荐阅读