【问题标题】:Is there any other method in Laravel, like whereJsonContains to search inside a json column?Laravel 中是否还有其他方法,例如 whereJsonContains 在 json 列中搜索?
【发布时间】:2021-06-02 15:34:51
【问题描述】:

json列socials的结构如下:

{"1":{"type":"Facebook","url":"fb.com\/rahul"},"2":{"type":"Twitter","url":"twt.com\/rahul"}}

在模型内部,socials 列被转换为AsArrayObject::class

所以,我想获取所有 "type" : "Facebook" 出现在社交列中的模型。 为此,我尝试使用whereJsonContains,如下所示:

public function index()
    {
        return Creator::whereJsonContains('socials.type','Facebook')->get();
    }

但是这段代码给出了空值。帮助我获取所有 "type" : "Facebook" 出现在社交列中的模型。

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    您应该使用-> 运算符查询您的列,而不是点符号。请参阅:https://laravel.com/docs/8.x/queries#json-where-clauses(第一段)。

    此外,您的查询语法与您的数据集不匹配,因为您尝试访问数据集中的第一级属性,而您的数据集是嵌套的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-29
      • 2022-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2018-04-30
      • 2023-02-20
      • 1970-01-01
      相关资源
      最近更新 更多