dpdhnd3577 2017-03-11 10:41
浏览 96
已采纳

在Laravel 5.4列中应用字符串函数

I'm using the latest Laravel 5.4

I am trying to make a simple query to search users by name. The query written for MySQL looks like this:

SELECT *
FROM users
WHERE upper(name) LIKE '%FOO%';

I'm trying to make it work with Eloquent. Things I've tried but failed:

  1. User::where('upper(name)', 'LIKE', '%FOO%')->get()

  2. DB::table('users')->where('upper(name)', 'LIKE', '%FOO%')->get()

Both fail with the following error:

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'upper(name)' in 'where clause' (SQL: select * from users where upper(name) LIKE %FOO%)'

The query seems to fail because Eloquent wraps the upper(email) statement with backticks (" ` ", " ` "). Is there a way to go around this issue or do I have to use a particular eloquent function to get convert a column to uppercase, lowercase, e.t.c?

  • 写回答

2条回答 默认 最新

  • duangu9173 2017-03-11 10:44
    关注

    Use DB::raw()

    User::where(DB::raw('upper(name)'), 'LIKE', '%FOO%')->get()
    

    It would generate query like this

    "select * from `users` where upper(name) LIKE ?"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问