Laravel : advanced search form query

11,179

Solution 1

You need to change:

if ($name)
   $query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");

into:

if ($name) {
   $query->where(function($q) use ($name) {
         $q->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");
   });
}

to make Laravel to add parentheses so it will work as you expect.

EDIT

Of course you don't need to wrap everything with closure here, so the best solution for that would be:

<?php

$query = DB::table('mytable')->select('id', 'first_name', 'last_name');

if ($name) {
    $query->where(function ($q) use ($name) {
        $q->where('first_name', 'like', "$name%")
            ->orWhere('last_name', 'like', "$name%");
    });
}
if ($specialty_s) {
    $query->where('primary_specialty', $specialty_s);
}
if ($city_s) {
    $query->where('city', $city_s);
}
if ($state_s) {
    $query->where('state_province', $state_s);
}
if ($lundbeck_id_s) {
    $query->where('customer_master_id', $lundbeck_id_s);
}
if ($degree_s) {
    $query->where('primary_degree', $degree_s);
}

$data = $query->get();

Solution 2

i will build the query little bit differently.

$q = DB::table('mytable')->select(['id', 'first_name', 'last_name']);

if(!empty($name))
{
    $q->where(function($query)use($name){
        $query->where(('first_name', 'like', "$name%")
              ->orWhere('last_name', 'like', "$name%");
    });
}

if(!empty($specialty_s) $q->where('primary_specialty', $specialty_s);

so far.... so forth.

at last

return q->get() or $q->paginate(30) depending upon your needs.

Share:
11,179
maximus 69
Author by

maximus 69

Updated on July 21, 2022

Comments

  • maximus 69
    maximus 69 almost 2 years

    I have an advanced search form to filter out results from a database using Laravel. The data is filtered correctly but I have a requirement for the user to be able to filter by first name or last name using the same text box (in the advanced form). I tried orWhere to make sure it filters the name field with the first name or last name but the orWhere doesn't consider the other filters. The code I am using is as follows:

    DB::table('mytable')
                    ->where(function($query) use ($name, $degree_s, $specialty_s, $city_s, $state_s, $lundbeck_id_s) {
    
                    if ($name)
                        $query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%"); # this is whats causing the issue
                    if ($specialty_s)
                        $query->where('primary_specialty', $specialty_s);
                    if ($city_s)
                        $query->where('city', $city_s);
                    if ($state_s)
                        $query->where('state_province', $state_s);
                    if ($lundbeck_id_s)
                        $query->where('customer_master_id', $lundbeck_id_s);
                    if ($degree_s)
                        $query->where('primary_degree', $degree_s);
                    })
    
                    ->select('id', 'first_name','last_name')
    

    Adding the orWhere clause causes the query to not use the other conditions as well (like city_s or state_s).

  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    It won't work as expected. It will create query where A or B AND C
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    You also forgot here about adding use ($name) to your closure
  • itachi
    itachi over 9 years
    having a bad day.... my mind is in stackoverflow i guess argh.... thank you for the corrections. again :)
  • maximus 69
    maximus 69 over 9 years
    Thanks a lot Marcin, this was the solution I had implemented
  • CodeGuru
    CodeGuru about 6 years
    What about pagination?