首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel搜索多个字段

Laravel搜索多个字段
EN

Stack Overflow用户
提问于 2017-02-28 12:00:04
回答 3查看 125关注 0票数 0

我现在有一个搜索功能在我的网站,我需要它搜索三个领域-应用,移动,电子邮件。

现在,一旦用户在搜索框中输入数据,它就会搜索所有的3个,但不起作用。

使用GET收集数据。

这是我的查询

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

代码语言:javascript
复制
$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();

因此,我需要它搜索每个字段,直到找到正确的字段值。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-28 12:25:36

代码语言:javascript
复制
$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();
票数 4
EN

Stack Overflow用户

发布于 2017-02-28 12:08:28

使用like搜索数据尝试

代码语言:javascript
复制
->when($mobile, function($query) use ($mobile){
     return $query->where('MobilePhone', 'like', '%'. $mobile . '%');
})

要实际搜索每个字段,请使用orWhere

代码语言:javascript
复制
$keywords = Input::get('searchall');
$data = DB::table('leads')
->where('mobilephone', '=', $keywords)
->orWhere('email', '=', $keywords)
->orWhere('AppID', '=', $keywords)
->get();
票数 0
EN

Stack Overflow用户

发布于 2017-02-28 12:18:17

我不明白为什么您需要使用3个变量从用户那里获得相同的值,所以下面是简化的版本:

代码语言:javascript
复制
$searched = Input::get('searchall');

$matched = DB::table('leads')
->where('MobilePhone', $searched)
->orWhere('Email', $searched)
->orWhere('AppID', $searched)
->get();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42508235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档