【发布时间】:2017-10-03 15:43:58
【问题描述】:
这是things 表的data 列中的JSON 值:
{a: [{b: 1}, {b: 2}]}
我可以通过这样的原始查询获取所有包含等于 1 的 b 的 things:
select * from things where data @> '{ "a": [{"b": 1}] }';
我知道我们可以使用带有 JSON where 子句的 Laravel 运行查询:https://laravel.com/docs/5.4/queries#json-where-clauses。我可以这样写:
Thing::where('a->c', 'foobar');
但是我可以写一个地方来检查a 是否包含{b: 1},就像在使用 Laravel 的查询构建器的原始查询中一样?
【问题讨论】:
-
这些“JSON where 子句”中的 seems that Laravel operates with
->和->>运算符(这不是您想要实现的)。但是 PostgresGrammar 直接支持@>和<@运算符,所以(理论上)你可以写->where('data', '@>', '{"a":[{"b":1}]}') -
@pozs 我刚刚检查过,你的“理论”有效!非常感谢我没有考虑这个,我正在写一个 whereRaw 查询。你可以发表你的评论作为答案,我会接受的:)
标签: postgresql laravel laravel-5 laravel-5.4 postgresql-json