简体   繁体   中英

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?

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. 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

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

from users where notification_preferences is null... , probably this is the reason why the update is not executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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