【问题标题】:Laravel Eloqeunt CAST() table column with postgres generating errorLaravel Eloquent CAST() 表列,postgres 生成错误
【发布时间】:2016-10-13 23:08:14
【问题描述】:

我正在使用带有 Postgres 的 Laravel 5.2。我有一个整数列,我需要在其中放置一个类似%% 的条件。我有一个保存整数值的 postal_code 列。现在我需要获取其中包含12 的所有行。但是我们不能用整数数据类型来做到这一点,所以我在进行查询时将其转换为文本,但我得到了错误。

这是我的代码

$query->where("cast(postal_code as TEXT)", "LIKE", "%".$request['postal_code']. "%");

它正在产生错误

"cast(postal_code" as "text)"

请查看不需要的" 正在查询中。这是查询输出错误。这是错误还是其他地方出了问题,我该如何解决。

【问题讨论】:

    标签: postgresql laravel


    【解决方案1】:

    我认为在 laravel 中的 where 上强制转换字段似乎不起作用。使用 whereRaw 函数执行原始查询。

    $query->WhereRaw("cast(postal_code as TEXT) ILIKE '%?%',[$request['postal_code']]");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 2014-05-13
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多