I am trying to query a JSON column in MYSQL using Laravel. I cannot get it to pick up records when I know there should be.
This is my select statement if I try it directly in MySQL after extracting from Laravel:
select `resources`.*, `users`.* from `resources` inner join `users` on `resources`.`user_id` = `users`.`id` where `status_id` = 3 and `resources`.`languages`->'$.""$"'.`name"` = English
I tried cleaning it up a bit according to the MySQL documentation:
select `resources`.*, `users`.* from `resources` inner join `users` on `resources`.`user_id` = `users`.`id` where `status_id` = 3 and `resources`.`languages`->"$.name" = "English"
My actual Laravel code is this:
$query = DB::table('resources')
->join('users', 'resources.user_id', '=', 'users.id')
->select('resources.*', 'users.*')
->where('status_id', '=', 3);
if (isset($languagesearch)) $query=$query->where('resources.languages->"$.name"', $languagesearch);
if (isset($formatssearch)) $query=$query->whereRaw('JSON_EXTRACT(resources.formats, "$.name") = "'.$formatssearch.'"');
$resources = $query->get();
$resources = json_decode( json_encode($resources), true);
You I have two JSON columns and I have tried them both using two different methods but still no luck. It does set or unset the value correctly, it is just the where clause that is not working.
EDIT: The JSON Column format is: [{"name": "English"}, {"name": "German"}, {"name": "Portuguese"}]
My MySQL version is 5.7.18