Search code examples
mysqljsonlaraveleloquentlaravel-5.6

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?


Solution

  • Just use a SQL LIKE operator

    ->where('desc', 'like', '%"club": "CHE"%');