laravel - 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
notifications
wheretype
= App\Notifications\SellerAnswerNotification anddata
->'$."question_id"' = 2079107489)
我也尝试过以下查询:
$result=DB::table('notifications')->whereRaw("JSON_EXTRACT(data, '$.question_id') = ?", [2079107489]);
但得到了类似的错误
如何更正查询以在通知表中按 json 获取行和删除行?
解决方案
给你,我试过这个,它奏效了:
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子句
推荐阅读
- python - Transformers 库中 Pegasus 模型的单词/句子的最大输入长度
- office-js - How to place outlook add-in in email itself for read email form
- java - How to connect web dashboard with android application and send data
- go - How to compare a type defined as a string with a string
- jquery - Django ajax 分页无法正常运行
- java - Populating values for pojo according to environment variables in spring
- reactjs - React Error: Warning: Each child in a list should have a unique "key" prop
- unity3d - how to find unity app icon's guid via script?
- sublimetext3 - Sublime Text 3:仅在一个视图中隐藏标签栏
- scala - IntelliJ 在返回类型为“Int”的方法末尾显示“:Unit”