4

data这是表列中的 JSON 值things

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

我可以使用这样的原始查询获取所有things包含b等于 1 的 a :

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

我知道我们可以使用 Laravel 使用带有 JSON where 子句的 Laravel 运行查询:https ://laravel.com/docs/5.4/queries#json-where-clauses 。我可以写如下内容:

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

但是我可以用 Laravel 的 Query Builder 写一个 where 来检查是否a包含{b: 1}就像在原始查询中一样吗?

4

1 回答 1

1

Laravel(截至目前)在这些“JSON where 子句”(至少对于 PostgreSQL)中使用->and运算符进行操作。->>这不是你想要达到的。

但是直接PostgresGrammar支持@>and<@运算符,所以你可以写:

Thing::where('data', '@>', '{"a":[{"b":1}]}')
于 2017-05-05T09:31:44.927 回答