JSON Where is the suggestion on an array of values ​​with Query Builder

Here is the JSON value in the datatable column things:

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

I can get everything thingscontaining bequal to 1 with a raw query like this:

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

I know that we can run a query with Laravel using JSON, where is the sentence with Laravel: https://laravel.com/docs/5.4/queries#json-where-clauses . I can write something like:

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

But can I write where to check if it contains a {b: 1}, as in a raw query with Laravel Query Builder?

+4
source share
1 answer

Laravel ( ) -> ->> "JSON where clauses" ( , PostgreSQL). , .

PostgresGrammar @> <@ , :

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

Source: https://habr.com/ru/post/1676451/


All Articles