Skip to content
Advertisement

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
}

Advertisement

Answer

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
    ]
  ]
]])
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement