I have multiple jsonb data fields in my postgres database. Like my Contacts table has name, addresses, phones and emails as jsonb data fields. Most of the jsonb data fields are multidimensional. Like emails field have different json data for different email types. Following is the sample emails data i have in my database.
[
{"tag": "Home", "value": "[email protected]"},
{"tag": "Other", "value": "[email protected]"},
{"tag": "Work", "value": "[email protected]"},
{"tag": "Home", "value": "[email protected]"}
]
I want to execute a search on emails field's value data. Like i want to fetch all the contacts with email [email protected] in emails field.
I have tried to fetch the contacts with normal where query like
$contacts = Contact::where('emails->value', 'ilike', "%$query%")->get();
But it does not return anything and that is obvious because emails field is multidimensional. Although on a single dimensional json data this query works fine.
What is the proper way of searching such data fields in laravel eloquent ?. It will be better if it can be done without using whereRaw with jsonb_exists function.