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?

2
  • data column is JSON or VARCHAR? Commented Aug 13, 2019 at 5:58
  • Example of data column you have in first code block. If you think about type of column, it's a text Commented Aug 13, 2019 at 6:14

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

forHow can I do that in Laravel for query building?
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();

1 Comment

sorry for the delay, but in case you still stuck with this problem, I think further link help you stackoverflow.com/questions/48016520/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.