Home > Software engineering >  Laravel querybuilder looking for Value in joined columns
Laravel querybuilder looking for Value in joined columns

Time:07-30

As I am new to Laravel and it's query builder, but know that Laravel is pretty insane in regards of functionality, I wanted to ask if it was possible to look for a value for example "Peter Smith" as a single string, which is saved in two seperate columns "first_name" and "last_name" respectively.

where('partners.first_name', 'like', '%' . $search . '%')
                    ->orWhere('partners.last_name', 'like', '%' . $search . '%'),

Currently I use this, which works, but only looks for the value in each one of those two columns. I need it combined though and if possible in the query statement.

Thanks in advance!

CodePudding user response:

1.add new field full name 2.save their value '$request->first_name' . '$request->last_name'

CodePudding user response:

As explained a similar situation in this link:
how to search several columns in a sql query using concat and UPPER
you can write a query like this:

$query = "SELECT * FROM partners WHERE CONCAT(first_name,' ',last_name) LIKE '%$search%'";

Adjust it with the Laravel Quer Builder. I assumed that there is a space between first name and last name, I tested in MySQL and got the result I wanted.

  • Related