natmed91
natmed91

Reputation: 47

Query for multiple fields with Laravel

So I am trying to create a search functionality so that you can query for both first and last name. What can I use as an AND statement for the query?

public function find()
{

    $search = Input::get('contact_search');
    $query = Contact::orderBy('name', 'desc');
    if (is_null($search)) 
    {

    $contacts = $query->paginate(15);
    //return View::make('contacts.index')->with(array('contacts' => $contacts));
    }   else {
    $contacts = $query->where('firstName', 'LIKE', "%{$search}%")
                   ->orWhere('lastName', 'LIKE', "%{$search}%")
                   ->paginate(15);
    $contact = Contact::find(1);

    }
    return View::make('hello')->with(array('contacts' => $contacts));

}

I have tried

$query->where('firstName', 'LIKE', "%{$search}%")
                   ->Where('lastName', 'LIKE', "%{$search}%")

but that does not work either. Any advice would be awesome! Thanks.

Upvotes: 2

Views: 4108

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

If you use where it will add AND if you use multiple where. However I don't know if in your case you want to use AND because you if someone will put into form Jo you will search for poeple that hat Jo both in name and surname, so for example John Smith won't be found here because his surname doesn't contain Jo.

So answering your question you could use:

$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
                   ->where('lastName', 'LIKE', "%{$search}%")
                   ->paginate(15);

but probably this won't make much sense.

It's hard to also say what exactly you do here, because you have here:

$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
                   ->orWhere('lastName', 'LIKE', "%{$search}%")
                   ->paginate(15);
$contact = Contact::find(1);

using those 2 lines first you look for people who have $search in first or last name and paginate them, and when using Contact::find(1); you find person with id 1. It also doesn't seem to be good solution to anything here.

If you would like to find the first record that have $search either in first or last name, you should use:

$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
                   ->where('lastName', 'LIKE', "%{$search}%")
                   ->first();

without orWhere and without paginate.

Upvotes: 1

Jake Wilson
Jake Wilson

Reputation: 91193

where() acts as an and statement already. Just chain them together.

$people = People::where('first_name','=','John')->where('last_name','=','Doe')->get();

In your query, you have ->Where. Make sure its lowercase.

Also, your method could use some optimization. You are searching for multiple contacts and paginating the results, but then you are doing a find(1) for some reason. A better approach is to just do the following:

$contact = Contact::orderBy('name','desc')
  ->where('firstName', 'LIKE', "%{$search}%")
  ->where('lastName', 'LIKE', "%{$search}%")
  ->first();

That will return your first contact in the results. No need for pagination. And find() actually searches for records based off of id's anyways so you don't want to use that in this case.

Upvotes: 2

Related Questions