首页 > 解决方案 > Laravel 8 更新 JSON 列值

问题描述

我正在尝试为json我的所有用户更新列类型的值,我通过 Tinker 运行的查询没有给出任何错误,它只是返回0并且列保持不变,我做错了什么?

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

我的行上的列当前具有...的值

{
    "expiry_alerts": true
}

标签: phplaravel

解决方案


在我阅读了下面的评论之后,我很清楚您对 JSON 列的 WHERE 查询使用了错误的语法。您必须使用 ->运算符。

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

它应该像这样工作:

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

推荐阅读