Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MarvinX's avatar

Is it possible to use a wildcard key in a JSON query?

I need to query for a model where any json value matches the given parameter. All entries have the same keys even though some keys might be missing. The data is provided by another package so I cannot transfer the data into a separate table.

I currently do it like this but it looks really bad to me:

foreach ($keys as $key) {
    $query->orWhere("{$field}->{$key}", $value);
}

I'd love to make a query similar to this:

$query->where("{$field}->.*", $value);

Example data:

{
	a: "value-1",
    b: "value-2",
    c: "value-3"
}
0 likes
4 replies
martinbean's avatar

@marvinx No. Store your data properly, i.e. relationally if you want to query it effectively.

If you’re just going to smash your data in a JSON column then you may as well just store it in a JSON file on disk instead of in a relational database.

1 like
MarvinX's avatar

@Sinnbeck Do you think my approach would be 'acxeptable' for 3 locales or would you create a separate table?

Please or to participate in this conversation.