dou72260 2017-02-17 08:16
浏览 34
已采纳

在查询中使用连接列与Eloquent

I am creating an api to search users. I am doing this in Eloquent, and I am wondering if there is any way to create a query with Eloquent like this:

SELECT * FROM users WHERE CONCAT('first_name', ' ', 'last_name') LIKE '%searchstring%' OR email LIKE '%searchstring%';

Is there a way to do this with the Eloquent query builder?

What I would like to do is essentially this:

$users = User::where('CONCAT(first_name, " ",last_name)', 'LIKE', '%searchstring%')->orWhere('email','LIKE','%searchstring%')->orderBy($ob, $dir)->paginate($perPage);

This results in Column not found: 1054 Unknown column 'CONCAT(first_name," ",last_name)' in 'where clause'

  • 写回答

2条回答 默认 最新

  • douke9379 2017-02-17 08:21
    关注

    I was able to find the answer to this question, and the solution was to use the DB::raw method around the column name. The working solution looks like this:

    $users = User::where(DB::raw('CONCAT(first_name, " ",last_name)'), 'LIKE', '%searchstring%')->orWhere('email','LIKE','%searchstring%')->orderBy($ob, $dir)->paginate($perPage);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?