Laravel 5.2中的MySQL concat()

时间:2016-03-30 15:08:45

标签: laravel

我正在尝试在Laravel中编写这个MySQL查询:

- name: copy shell: cp /home/user/test1.txt /home/user/Desktop/test1.txt

我们的想法是返回SELECT * FROM users WHERE CONCAT(firstName, ' ', lastName) LIKE '%John%'firstName lastName的记录

我尝试过的事情:

John

错误 $users = User::whereRaw("(CONCAT(users.firstName, ' ', users.lastName) like '%John%')");

The Response content must be a string or object implementing __toString(), "boolean" given.

返回$users = User::whereRaw("(CONCAT(users.firstName, ' ', users.lastName) like '%?%')",[$search])->get();

[]

返回$users = User::whereRaw("(CONCAT(users.firstName, ' ', users.lastName) like ?)",[$search])->get();

2 个答案:

答案 0 :(得分:0)

检查SQL之后(感谢@xAoc!)我发现生成的SQL错误。

这似乎生成了正确的SQL:

return User::whereRaw("(CONCAT(users.firstName, ' ', users.lastName) like '%$search%')")->get();

答案 1 :(得分:0)

对我来说,你可以试试这个:

$search = 'John Smith';
$users = User::whereRaw("(CONCAT(first_name, ' ', last_name) like '%$search%')")->get();

输出如下:

  

收藏{#573▼         #it​​ems:array:13 [▶]       }