Filter Records by JSON Array Size with whereJsonLength()

Punyapal Shah
Use whereJsonLength() to query database records based on the number of elements in a JSON array attribute.

Filtering records by the number of elements stored in a JSON array column is straightforward in Eloquent using whereJsonLength().

use App\Models\User;

// Select users with more than 2 items in their JSON options->tags array
$users = User::whereJsonLength('options->tags', '>', 2)->get();
  • Queries database JSON array length using native database JSON functions
  • Supports comparison operators (=, >, <, >=, <=)
  • Works with MySQL, PostgreSQL, and SQLite database drivers
Tags: #Laravel #Eloquent #Database
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips