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条答案
按热度按时间tcbh2hod1#
A possible approach is to use
whereRaw()
method to build a rawWHERE
clause for your query. The actual statement depends on the JSON structure and in your case you need twoOPENJSON()
calls to parse the nested JSON content:As an additional note, if the JSON content has this fixed structure (two nested arrays, the first one containing a single JSON array and the second one with a single JSON object as item), you may simplify the statement:
f4t66c6m2#
You can use the
JSON_VALUE
function to extract the 'phone_number' value from the JSON dataao218c7q3#
You can use
whereJsonContains()
instead ofwhere()
.