【发布时间】:2018-08-14 14:29:20
【问题描述】:
我有一个搜索输入?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 . "%");
Customers 已经定义,是一个 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)
【问题讨论】:
-
您是否尝试过常规字符串连接? ->whereraw("COALESCE(first_name + ' ' + last_name, '') LIKE '%$query%'").我不太擅长 SQL,所以我不确定 ILIKE 和 LIKE 是什么,但这是我们的名字和姓氏搜索功能,效果很好。
-
这很有趣。 sql 看起来有效,除了列名周围的双引号,不知道为什么它会抛出未定义的列。然而,很奇怪,laravel 在列名周围添加了双引号。他们不应该在那里。
-
试试这个:
$result = Customers::where(DB::raw('concat(first_name," ",last_name)') , 'LIKE' , '%keyword%')->get();让我们知道它是否有帮助。 -
你用的是什么数据库?
-
我正在使用 Postgresql