0

I have a table "Notifications". In my data column, I have an array-like:

{  
   "title":"Something",
   "arr":{  
      "id":12,
      "name":"HelloWorld",
      "avatar":null
   }
}

And I want to check element (notify) which arr->id == 12 for example (or something else). I tryed make query buildng but nothing works. Examples:

$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
                             ->where('data', '%12%')->first();

or

$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)
                              ->where('data->arr->id', '12')->first();

Who knows how do that?

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64

2 Answers2

0

You can use this ..SELECT JSON_SEARCH(......) for more reference you can refer this link How to search JSON array in MySQL?

0

I think this would help:

$arr = 'arr';
$id = 'id';
$hello = auth()->user()->unreadNotifications->where('notifiable_id', Auth::user()->id)->where("data->{$arr}->{$id}", '12')->first();
  • sorry for the delay, but in case you still stuck with this problem, I think further link help you https://stackoverflow.com/questions/48016520/making-a-laravel-5-4-query-on-a-json-field-containing-a-json-array – Mohamad Karimisalim Aug 14 '19 at 14:27