【问题标题】:Making a query on a table that has json field and postgresql database and laravel对具有 json 字段和 postgresql 数据库和 laravel 的表进行查询
【发布时间】:2021-08-17 19:25:57
【问题描述】:

我有一个表,其 has 列包含一个 json 字段和 postgresql 作为包含此示例的数据库存储:

{"wants_newsletter": "true", "favorite_color": "red"}

如果我像这样在 laravel 上查询:

$model = Model::where('column->favorite_color', 'red')->first();

我可以成功获取该行,现在可以这样做

$id = $model->id;

但是当在同一列有这种json结构

{"wants_newsletter": ["true"], "favorite_color": ["red","blue"]}

如果我像这样在 laravel 上查询:

$model = Model::where('column->favorite_color[0]', 'red')->first();

我得到$model 为空,这意味着我没有获取一行。

在laravel中怎么做?

我在搜索时找到的资源是: How can I make query where json column in the laravel? https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/

【问题讨论】:

    标签: php laravel postgresql


    【解决方案1】:

    我认为您正在寻找的是:

    ->whereJsonContains('column->favorite_color', 'red')->first();
    

    https://laravel.com/docs/8.x/queries#json-where-clauses

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      相关资源
      最近更新 更多