JSON Where Clauses
To query a JSON column, use the ->
operator:
$users = DB::table('users')
->where('options->language', 'en')
->get();
$users = DB::table('users')
->where('preferences->dining->meal', 'salad')
->get();
You may use whereJsonContains to query JSON arrays (not supported on SQLite):
$users = DB::table('users')
->whereJsonContains('options->languages', 'en')
->get();
MySQL and PostgreSQL support whereJsonContains
with multiple values:
$users = DB::table('users')
->whereJsonContains('options->languages', ['en', 'de'])
->get();
You may use whereJsonLength
to query JSON arrays by their length:
$users = DB::table('users')
->whereJsonLength('options->languages', 0)
->get();
$users = DB::table('users')
->whereJsonLength('options->languages', '>', 1)
->get();