PostgreSQL – JSON WHERE uses Query Builder to CLAUSE

This is the JSON value in the data column of a thing table:

{a: [{b: 1} , {b: 2}]}

I can use the original query like this to get all the content containing b equal to 1:

select * from things where data @>'{ "a": [{"b": 1}] }';

I know we can use Laravel: https://laravel.com/docs/5.4 /queries#json-where-clauses’ JSON where clause runs Laravel queries. I can write it like this:

Thing::where('a->c','foobar ');

But, can I write a place containing {b:1} in the original query of Laravel’s query builder?

Laravel (as of now) use> to operate. And – >>operator in these ” JSON where clause” (at least for PostgreSQL). This is not what you want to achieve.

But PostgresGrammar supports @> and <@ operators directly, so you can write:

Thing::where('data','@>','{"a":[{"b":1}]}')

This is the JSON value in the data column of a thing table:

{a: [{b: 1}, {b: 2}] }

I can use this original query to get all the content containing b equal to 1:

select * from things where data @>'{ "a": [{"b": 1}] }';

I know we can use Laravel: https://laravel.com/docs/5.4/queries#json-where- The JSON where clause of clauses runs Laravel queries. I can write it like this:

Thing::where('a->c','foobar');

But, can I write a place that contains {b:1} in the original query of Laravel’s query builder?

Laravel (as of now) uses> to operate. And->> operator in these “JSON where clauses” (at least for PostgreSQL ). This is not what you want to achieve.

But PostgresGrammar supports @> and <@ operators directly, so you can write:

Thing::where( 'data','@>','{"a":[{"b":1}]}')

Leave a Comment

Your email address will not be published.