【问题标题】:Using Json properties in where clause laravel在 where 子句 laravel 中使用 Json 属性
【发布时间】:2020-09-18 03:54:24
【问题描述】:

我有这张表discounts,我有一个名为rules 的列,它是一个json,它里面有book 数组,就像这样

餐桌折扣:

Column Rules:

rules:"{\"books\":[1,2],others..} 

现在我想通过 Books 表检查该特定书籍是否包含在任何 discounts->rules->books 中!

我试过的是这样的:

$discount = Discount:: whereJsonContains('rules->books',$this->id)->get();

但这给我返回的是空数组。

【问题讨论】:

标签: php mysql json database laravel


【解决方案1】:

数据类型必须匹配:

// [1, 2]
Discount::whereJsonContains('rules->books', 1)   // Works.
Discount::whereJsonContains('rules->books', '1') // Doesn't work.

// ["1", "2"]
Discount::whereJsonContains('rules->books', '1') // Works.
Discount::whereJsonContains('rules->books', 1)   // Doesn't work.

【讨论】:

  • 我认为数据类型匹配你能看到我使用的代码 $this->id 我认为这会像 1 而不是 "1" 对吗?
  • 只有一种方法可以检查!与dd(gettype($variable))一起死和转储
猜你喜欢
  • 2017-08-05
  • 2019-09-08
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多