使用Eloquent查询JSON数组

时间:2017-02-22 23:13:23

标签: php laravel-5.3

在Laravel 5.3中,我在一个名为_days的postgres数据库中有一个JSON列。

我尝试查询如下所示:

$day=3
//_days = '[true,false,false,true,true,false,true]'
$query->where('_days->'.$day,'true')->first();

我要做的是检查json数组的索引3是否为真。 不幸的是,它返回 null

当获取eloquent查询的sql时,我发现它生成了以下SQL:

select * 
from "table" 
where "_days"->>'3' = true

这当然不起作用,因为它试图在没有的情况下找到关键字“3”。

我想要的查询是:

select * 
from "table" 
where "_days"->>3 = true

如何通过重复创建原始查询并使用雄辩来实现这一目标?

1 个答案:

答案 0 :(得分:0)

我想我会用JSON专栏创建一个集合,然后像这样得到索引3的那一天

collect(json_decode('[true,false,false,true,true,false,true]'))->get($day)