Laravel - Query Builder - Json Where Clauses

Laravel also supports querying JSON column types on databases that provide support for JSON column types. Currently, this includes MySQL 5.7+, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (with the JSON1 extension). To query a JSON column, use the -> operator:

    
    $users = DB::table('users')
                    ->where('preferences->dining->meal', 'salad')
                    ->get();
	

You may use whereJsonContains to query JSON arrays. This feature is not supported by the SQLite database:

    
    $users = DB::table('users')
                    ->whereJsonContains('options->languages', 'en')
                    ->get();
	

If your application uses the MySQL or PostgreSQL databases, you may pass an array of values to the whereJsonContains method:

    
    $users = DB::table('users')
                    ->whereJsonContains('options->languages', ['en', 'de'])
                    ->get();
	

You may use whereJsonLength method 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();