Laravel:如何通过data-> postid删除数据库通知行

时间:2018-04-08 09:50:45

标签: php laravel mariadb

我已经按照此链接在我的laravel博客应用中创建数据库通知,当一个用户喜欢其他用户的帖子时

https://laravel.com/docs/5.5/notifications#database-notifications

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

表格:通知

  

id |类型| notifiable_id | notifiable_type |数据| read_at |     created_at |的updated_at

id = 0b2a7fdf-eea4-4982-a86d-e874bb4f28ef

type = App\Notifications\PostLiked

notifiable_id= 48 

data = { "event":"LIKE",
         "postid":17,
         "sender":{
                   "id":50,
                   "name":"Developer",
                  }
       }

read_at = NULL

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

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

现在我想通过data-> postid

删除数据库通知行

我在下面尝试过查询:

DB::table('notifications')
    ->where('type','App\Notifications\PostLiked')
    ->where('data->postid',17)
    ->first();

得到错误:

  

QueryException SQLSTATE [42000]:语法错误或访问冲突:1064   您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   靠近'' $。" postid"' = 17)限制1'在第1行(SQL:select * from   通知where(type = App \ Notifications \ PostLiked   和数据 - >' $。" postid"' = 17)限制1)

我也试过以下查询:

  

$结果= DB ::表('通知') - > whereRaw(" JSON_EXTRACT(数据,   ' $ .postid')=?",[17]);

{
connection: { },
grammar: { },
processor: { },
bindings: {
select: [ ],
join: [ ],
where: [
17
],
having: [ ],
order: [ ],
union: [ ]
},
aggregate: null,
columns: null,
distinct: false,
from: "notifications",
joins: null,
wheres: [
{
type: "raw",
sql: "JSON_EXTRACT(`data`, '$.postid') = ?",
boolean: "and"
}
],
groups: null,
havings: null,
orders: null,
limit: null,
offset: null,
unions: null,
unionLimit: null,
unionOffset: null,
unionOrders: null,
lock: null,
operators: [
"=",
"<",
">",
"<=",
">=",
"<>",
"!=",
"<=>",
"like",
"like binary",
"not like",
"between",
"ilike",
"&",
"|",
"^",
"<<",
">>",
"rlike",
"regexp",
"not regexp",
"~",
"~*",
"!~",
"!~*",
"similar to",
"not similar to",
"not ilike",
"~~*",
"!~~*"
],
useWritePdo: false
}

请建议我正确查询以通过通知表

中的json(data.key)获取行和删除行

3 个答案:

答案 0 :(得分:1)

MariaDB不支持这种用于查询JSON列的表示法。

您可以使用原始SQL:

->whereRaw("JSON_EXTRACT(`data`, '$.postid') = ?", [17])

或者安装此软件包:https://github.com/ybr-nx/laravel-mariadb

答案 1 :(得分:0)

DB::table('notifications')
    ->where('type','App\Notifications\PostLiked')
    ->where('data->postid',17)
    ->first();

我猜这里的错误是 where(&#39; data-&gt; postid&#39;,17)。查看其中(&#39; name_column_in_table&#39;,&#39;值&#39;); data-&gt;帖子

中不能包含字段名称

答案 2 :(得分:0)

对我有用。您可以使用以下查询:

textPdf
相关问题