我有一个搜索输入?search=xxxx
我的数据库first_name
和last_name
中有两列。
我希望搜索对它们都起作用,例如,如果我的输入是John Smith
搜索应该像CONCAT(first_name, last_name) ILIKE %input%
但是laravel不允许我这样做:
$customers = $customers->selectRaw("CONCAT('first_name', 'last_name') AS fullname")->where("fullname", "ilike", "%" . $text . "%");
客户已经定义,并且是ORM对象,而不是集合。
我为此得到的错误:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "fullname" does not exist LINE 1: ...) AS fullname from "people" where "type" = $1 and "fullname"... ^ (SQL: select CONCAT('first_name', 'last_name') AS fullname from "people" where "type" = customer and "fullname" ilike %fidel% order by "id" asc)
也尝试过:
$customers = $customers->whereRaw("CONCAT('first_name', 'last_name')", "ilike", "%" . $text . "%");
另一个错误:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined (SQL: select * from "people" where "type" = customer %fidel% CONCAT('first_name', 'last_name') order by "id" asc)
答案 0 :(得分:1)
存在多个问题:
fullname
子句中的派生列,例如WHERE
。whereRaw()
仅具有两个参数,即SQL字符串和绑定数组。使用此:
$customers->where(DB::raw('concat("first_name", "last_name")'), 'ilike', '%'.$text.'%');
答案 1 :(得分:0)
根据您的代码编写方式,我不确定您使用的是什么,但是如果是sql,则可能是您想要的...
DECLARE @cust_name VARCHAR(100) = 'john smith' -- users input
DECLARE @first_name VARCHAR(50) = SUBSTRING(@cust_name, 1, CHARINDEX(' ', @cust_name) - 1)
DECLARE @last_name VARCHAR(50) = SUBSTRING(@cust_name, CHARINDEX(' ', @cust_name) + 1, 8000)
SELECT CONCAT(c.FirstName,c.LastName)
FROM
customers c
WHERE
c.FirstName = @first_name
AND c.LastName = @last_name