2017-02-28 209 views
0

我目前在我的网站上有一个搜索功能,我需要它来搜索3个字段 - appid,mobilenumber,email。Laravel搜索多个字段

现在,用户在搜索框中输入数据后,它将搜索所有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();