【问题标题】:My whereJsonContains not working (Laravel 5.8)我的 whereJsonContains 不起作用(Laravel 5.8)
【发布时间】:2021-08-14 01:51:16
【问题描述】:

我的 laravel 查询有问题。

现在我使用这样的查询:

$courses = Course::whereJsonContains('schedule->day', 1)->get();

它不起作用。

我使用的是 postgreSql 9.6,我的数据库和原始查询如下所示

http://sqlfiddle.com/#!17/88fd2/1/0

我想选择在第 1 天有时间表的班级

【问题讨论】:

  • whereJsonContains 假定 schedule->day 是一个整数数组。在您的情况下,它是父数组的属性。

标签: php laravel eloquent


【解决方案1】:

如果您将列定义为schedule->day,MySQL 假定这是一个整数数组。在您的情况下,它是一个对象数组,因此您必须定位父数组并在第二个参数中添加您要查找的属性名称。

像这样:

$courses = Course::whereJsonContains('schedule', ['day' => 1])->get();

【讨论】:

    【解决方案2】:

    我解决了

    $courses = Course::whereJsonContains('schedule', [['day' => '1']])->get();

    【讨论】:

      【解决方案3】:

      如果您查询的值您不需要使用whereJsonContains,只需使用常规的where 查询,例如:

      $courses = Course::where('schedule->day', 1)->get();`
      

      如果要检查Json中是否存在day,使用whereJsonLength如:

      $courses = Course::whereJsonLength('schedule->day', '>', 0)->get();
      

      【讨论】:

        【解决方案4】:

        我解决了

        $products=ProductShop::active()
                  ->whereJsonContains('tag', [['value' => "tampa"]])->get();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-02
          • 2019-01-03
          • 2020-03-04
          • 2020-09-14
          • 2020-12-13
          • 2020-02-19
          • 2020-12-14
          • 1970-01-01
          相关资源
          最近更新 更多