【问题标题】:Laravel 5.8 Model whereJsonContains returning only exact matchesLaravel 5.8 模型 whereJsonContains 只返回完全匹配
【发布时间】:2020-03-04 15:53:51
【问题描述】:

Laravel:5.8

我的whereJsonContains 查询目前存在一些问题。我正在尝试搜索一个对象,如果字符串是完全匹配或部分匹配,我会得到结果,目前我只能得到完全匹配。

$quote->whereJsonContains( 'quotes->current' , [ 'quote' => $request->input( 'quote' ) ] );

我删除了不必要的代码,因为这是搜索过滤器的一部分。其余的工作正常,但我似乎在文档中找不到任何关于这个或任何工作示例的内容。

数据库中的json对象示例:

{
    "current": [
      {
        "quote": "This is a test quote 1",
        "page": "7",
        "who": "Nancy Harris",
        "explanation": "This is a text explanation about quote 1",
        "tags": "test1, testing, test",
        "created": "2019-10-30T00:38:20.414818Z",
        "updated": "2019-10-30T00:38:20.414826Z"
      },
      {
        "quote": "This is a test quote 2",
        "page": "60",
        "who": "Agnes Bates",
        "explanation": "This is a text explanation about quote 2",
        "tags": "test1, testing, test",
        "created": "2019-10-30T00:38:20.414818Z",
        "updated": "2019-10-30T00:38:20.414826Z"
      },
      {
        "quote": "This is a test quote 3",
        "page": "11",
        "who": "Wayne Griffin",
        "explanation": "This is a text explanation about quote 3",
        "tags": "test1, testing, test",
        "created": "2019-10-30T00:38:20.414818Z",
        "updated": "2019-10-30T00:38:20.414826Z"
      }
    ]
  }

【问题讨论】:

  • 您是否尝试过将引号作为字符串作为第二个参数传递? $quote->whereJsonContains('quotes->current', request( 'quote'));
  • 不,我认为它不起作用或工作的值错误。

标签: php laravel eloquent laravel-5.8


【解决方案1】:

尝试扩展到您的 qoute 数据。

$quote->whereJsonContains( 'quotes->current->qoute' ,'like', request('quote'));

【讨论】:

  • 他要求完全匹配。
  • @DilipHirapara nope,基于他的问题:但我还需要返回部分结果,就像我使用 LIKE 运算符一样
  • 是的,我只得到完全匹配,我也需要部分匹配。会试一试。
  • 不走运,这会导致无效的 sql。 SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '%we% json_contains(quotes, ?, '$."current"."quote"')' 附近使用正确的语法
  • @Second2None 你试过去掉百分号吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多