1

I have a search input ?search=xxxx

I have two columns in my database first_name and last_name. I want the search to work for both of them, for example if my input is John Smith the search should be like CONCAT(first_name, last_name) ILIKE %input%

But laravel doesn't let me do it:

$customers = $customers->selectRaw("CONCAT('first_name', 'last_name') AS fullname")->where("fullname", "ilike", "%" . $text . "%");

Customers is already defined, and is an ORM object, not collection.

And the error i get for this:

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)

Also tried:

 $customers = $customers->whereRaw("CONCAT('first_name', 'last_name')", "ilike", "%" . $text . "%");

And another error:

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)
5
  • Have you tried just regular string concatenation? ->whereraw("COALESCE(first_name + ' ' + last_name, '') LIKE '%$query%'"). I'm not great with SQL, so I'm not sure what ILIKE is vs LIKE, but that's our search function for first and last name and it works pretty good. Commented Aug 14, 2018 at 14:34
  • This is interesting. The sql looks valid, except for the double quotes around column name, not sure why it would throw an undefined column. It's very strange however, that laravel add double quotes around the column names. They should not be there. Commented Aug 14, 2018 at 14:44
  • try this: $result = Customers::where(DB::raw('concat(first_name," ",last_name)') , 'LIKE' , '%keyword%')->get(); let us know if it's helpful. Commented Aug 14, 2018 at 15:18
  • What database are you using? Commented Aug 14, 2018 at 15:44
  • I am using Postgresql Commented Aug 14, 2018 at 15:44

2 Answers 2

3

There are multiple problems:

  • You can't wrap columns in single quotes. Use double quotes (or no quotes at all).
  • You can't access derived columns like fullname in the WHERE clause.
  • whereRaw() only has two parameters, the SQL string and an array of bindings.

Use this:

$customers->where(DB::raw('concat("first_name", "last_name")'), 'ilike', '%'.$text.'%');
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what it is you are using based on how you are writing your code but if it is sql this might be what you are after...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.