Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make query where json column in the laravel?

I use laravel 5.6

I have a field. The data type of the field is json

The value of the field (desc field) like this :

[
{"code": "1", "club": "CHE", "country": "ENGLAND"}, 
{"code": "2", "club": "BAY", "country": "GERMANY"}, 
{"code": "3", "club": "JUV", "country": "ITALY"}, 
{"code": "4", "club": "RMA", "country": "SPAIN"}, 
{"code": "5", "club": "CHE", "country": "ENGLAND"}, 
{"code": "6", "club": "BAY", "country": "GERMANY"}, 
{"code": "7", "club": "JUV", "country": "ITALY"}, 
{"code": "8", "club": "RMA", "country": "SPAIN"}, 
{"code": "CODE", "club": "CLUB", "country": "COUNTRY"}
]

I want to check the key of club have value "CHE" or not

I try like this :

->where('desc->club','=', 'CHE')->get();

But it does not work

How can I solve this problem?

like image 910
moses toh Avatar asked Dec 18 '22 00:12

moses toh


2 Answers

Just use a SQL LIKE operator

->where('desc', 'like', '%"club": "CHE"%');
like image 179
Christopher Francisco Avatar answered Dec 20 '22 18:12

Christopher Francisco


Try this:

->whereRaw('JSON_CONTAINS(`desc`, \'{"club":"CHE"}\')')
like image 26
Jonas Staudenmeir Avatar answered Dec 20 '22 17:12

Jonas Staudenmeir