I’m trying to fetch a record from MSSQL table based on field value which contains json data. Following is the sample column value for ‘updated_value’ column:
[[{"phone_number":"5555555555","phone_type":"H","inactive_date":null,"restrictions":["SU","F"],"start_time":null,"end_time":null}]]
My query is:
$existing = ContactChangeLogs::latest()
->where('updated_value->phone_number', '5555555555')
->first();
But dd($existing)
gives null
result.
3
Answers
You can use
whereJsonContains()
instead ofwhere()
.A possible approach is to use
whereRaw()
method to build a rawWHERE
clause for your query. You need twoOPENJSON()
calls to parse the nested JSON content:As an additional note, if the JSON content has a fixed structure (two nested arrays, the second one with a single item), you may simplify the statement:
You can use the
JSON_VALUE
function to extract the ‘phone_number’ value from the JSON data