2017-02-17 08:16
浏览 34


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'

图片转代码服务由CSDN问答提供 功能建议

我正在为搜索用户创建一个api。 我在Eloquent中这样做,我想知道是否有任何方法可以像这样使用Eloquent创建查询:

  SELECT * FROM users WHERE CONCAT('first_name',  '','last_name')LIKE'%searchstring%'或发送电子邮件LIKE'%searchstring%'; 

有没有办法用Eloquent查询执行此操作 构建器?


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

这导致找不到列:1054未知列'CONCAT(first_name,“”,last_name)'' 子句'

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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);
    解决 无用
    打赏 举报

相关推荐 更多相似问题