Laravel 查询生成器多列比较

Laravel query builder multiple column in where comparison

我正在将查询构建器与 laravel 一起使用,我正在尝试获得

的等价物
SELECT name
     , firstname 
FROM emp
WHERE name+' '+firstname LIKE '%test%'

在查询生成器中?我怎么会得到相同的结果?

你可以在 whereRaw() 中这样做

DB::table('emp')->whereRaw("name+' '+firstname LIKE '%test%'")->select('name', 'firstname')->get();

希望对你有用!

尝试对多列使用 CONCAT

DB::table('emp')->Where(DB::raw("CONCAT(`name`, ' ', `firstname`)"), 'LIKE', "%test%")->select('name', 'firstname')->get();

希望这会有所帮助。

 $result = DB::table('emp')->select('name', 'firstname')->where(DB::raw("CONCAT('name', ' ', 'firstname')"), 'LIKE', '%'. 'test' .'%')->get();