0

I currently have a search function on my website, I need it to search 3 fields - appid, mobilenumber, email .

Right now once the user enters the data in the search box it searches all 3 but its not working.

Data is collected using GET.

Here is my query

http://www.example.com?id=1&searchall=07853637362

$mobile = INPUT::get('searchall');
$email = INPUT::get('searchall');
$appid = INPUT::get('searchall');

$data = DB::table('leads')
->when($mobile, function($query) use ($mobile){
    return $query->where('MobilePhone', $mobile);
})
->when($email, function($query) use ($email){
    return $query->where('Email', $email);
})
->when($appid, function($query) use ($appid){
    return $query->where('AppID', $appid);
})
->get();

So I need it to search each field until it finds the correct field value.

1
  • Consider writing some manual SQL from time to time, that would make you know that there are OR clauses for something :) Commented Feb 28, 2017 at 12:07

3 Answers 3

4
$mobile = INPUT::get('searchall');
$email = INPUT::get('searchall');
$appid = INPUT::get('searchall');

$data = DB::table('leads')
->when($mobile, function($query) use ($mobile){
                return $query->orWhere('MobilePhone', $mobile);
            })

            ->when($email, function($query) use ($email){
                return $query->orWhere('Email', $email);
            })

            ->when($appid, function($query) use ($appid){
                return $query->orWhere('AppID', $appid);
            })->get();
Sign up to request clarification or add additional context in comments.

Comments

0

To search data try with like

->when($mobile, function($query) use ($mobile){
     return $query->where('MobilePhone', 'like', '%'. $mobile . '%');
})

to actually search each field, use orWhere

$keywords = Input::get('searchall');
$data = DB::table('leads')
->where('mobilephone', '=', $keywords)
->orWhere('email', '=', $keywords)
->orWhere('AppID', '=', $keywords)
->get();

Comments

0

I don't understand why you need the same value from the user using 3 variables, so the following is the simplified version:

$searched = Input::get('searchall');

$matched = DB::table('leads')
->where('MobilePhone', $searched)
->orWhere('Email', $searched)
->orWhere('AppID', $searched)
->get();

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.