简体   繁体   English

Laravel 8 更新 JSON 列值

[英]Laravel 8 update JSON column value

I'm trying to update the value of a json column type for all of my users, my query that I'm running through Tinker doesn't give any errors, it just returns 0 and the columns remain unchanged, what am I doing wrong?我正在尝试为我的所有用户更新json列类型的值,我通过 Tinker 运行的查询没有给出任何错误,它只是返回0并且列保持不变,我在做什么错?

User::where('notification_preferences')->update(['notification_preferences' => [
  'domains' => [
    'expiry' => [
      'mail' => true,
      'database' => true
    ]
  ]
]])

My columns on my rows currently has the value of...我的行上的列当前具有...的值

{
    "expiry_alerts": true
}

After I have read the comment below it came clear to me that you used the wrong syntax for the WHERE query of a JSON column.在我阅读了下面的评论之后,我很清楚您对 JSON 列的 WHERE 查询使用了错误的语法。 You have to use the -> operator.您必须使用->运算符。

For further information please see https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/ & https://laravel.com/docs/8.x/queries#json-where-clauses有关详细信息,请参阅https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/https://laravel.com/docs/8。 x/查询#json-where-clauses

It should work like this:它应该像这样工作:

User::where('notification_preferences->expiry-alerts',true)->update(['notification_preferences' => [
  'domains' => [
    'expiry' => [
      'mail' => true,
      'database' => true
    ]
  ]
]])

I think the problem is your where clause User::where('notification_preferences') , if you express this to sql User::where('notification_preferences')->toSql() , you will notice something like this我认为问题在于您的 where 子句User::where('notification_preferences') ,如果您将此表达为 sql User::where('notification_preferences')->toSql() ,您会注意到类似这样的内容

from users where notification_preferences is null... , probably this is the reason why the update is not executed. from users where notification_preferences is null... ,这可能是未执行更新的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM