这是 things
表的 data
列中的一个 JSON 值:
{a: [{b: 1}, {b: 2}]}
我可以通过这样的原始查询获取所有包含等于 1 的 b
的 things
:
select * from things where data @> '{ "a": [{"b": 1}] }';
我知道我们可以使用 Laravel 运行查询,使用 Laravel 的 JSON where 子句:https://laravel.com/docs/5.4/queries#json-where-clauses .我可以这样写:
Thing::where('a->c', 'foobar');
但是我可以写一个 where 来检查 a
是否包含 {b: 1}
就像在 Laravel 的查询生成器的原始查询中一样吗?
最佳答案
Laravel ( as of now ) 使用 ->
操作和 ->>
这些“JSON where 子句”中的运算符(至少对于 PostgreSQL)。这不是您想要实现的。
但是 PostgresGrammar
支持@>
和 <@
运算符直接,所以你可以写:
Thing::where('data', '@>', '{"a":[{"b":1}]}')
关于postgresql - 带有查询生成器的值数组的 JSON Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43800202/