2017-02-28 13 views
0

私は現在、私のウェブサイトに検索機能を持っています。私は3つのフィールド(appid、mobilenumber、email)を検索する必要があります。Laravel search mutiple fields

今すぐユーザーが検索ボックスにデータを入力すると、3つすべてが検索されますが、機能していません。

データはGETを使用して収集されます。ここで

は私のクエリは

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(); 

あるので、私はそれが正しいフィールド値が見つかるまで、それはそれぞれのフィールドを検索する必要があります。

+0

いくつかの手動SQLを書くことを検討してください。何かの理由でOR句があることがわかります:) – Borjante

答えて

2
$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(); 
0

、実際に各フィールドを検索するlike

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

で試すデータを検索orWhere

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

を使用するには、3つの変数を使用して、ユーザーから同じ値を必要とする理由私は理解していません、次は簡略版です:

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

$matched = DB::table('leads') 
->where('MobilePhone', $searched) 
->orWhere('Email', $searched) 
->orWhere('AppID', $searched) 
->get(); 
関連する問題