JSON Where Clauses

Laravel offers helpers to query JSON columns for databases that support them.

Currently, MySQL 5.7+, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (using the JSON1 extension)

1// To query a json column you can use the -> operator
2$users = User::query()
3 ->where('preferences->dining->meal', 'salad')
4 ->get();
5// You can check if a JSON array contains a set of values
6$users = User::query()
7 ->whereJsonContains('options->languages', [
8 'en', 'de'
9 ])
10 ->get();
11// You can also query by the length a JSON array
12$users = User::query()
13 ->whereJsonLength('options->languages', '>', 1)
14 ->get();

Tip given by @cosmeescobedo

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord