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 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要