首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5.3高级搜索查询

问Laravel 5.3高级搜索查询
EN

Stack Overflow用户
提问于 2017-02-27 11:45:12
回答 2查看 310关注 0票数 1

在我的Laravel 5.3项目中,我有一个带有一个文本框(关键字)和两个选择下拉框(类别和位置)的表单。我试图在我的控制器中构建select子句,但是当我为table_1.category或table_1.location设置where时,它就不起作用了。

代码语言:javascript
复制
    $keys       = \Request::get('keyword');
    $category   = \Request::get('category');
    $location   = \Request::get('location');

    $myrecord = mytable_1::join('table_2', 'table_2.user_id', '=', 'table_1.user_id')
        ->where(function ($query) {
            $keys = \Request::get('keyword');
            $query->where('table_1.title', 'like','%'.$keys.'%')
                  ->orWhere('table_2.company', 'like','%'.$keys.'%');
        })
        ->where('table_1.category', $category)
        ->where('table_1.location', $location)
        ->select('table_2.*', 'table_1.*')
        ->paginate(30);

请告诉我实现这一目标的方法。谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-02-27 13:02:50

你需要使用' use ()‘方法将一个参数传递给你的where query...if,你的参数包含数组,然后你可以使用foreach循环。我想它会起作用的..。

代码语言:javascript
复制
    $keys  = \Request::get('keyword');
    $category   = \Request::get('category');
    $location   = \Request::get('location');

    $myrecord = mytable_1::join('table_2', 'table_2.user_id', '=','table_1.user_id')
    ->where(function($query) use ($keys){
        $query->where('table_1.title', 'like','%'.$keys.'%')
        ->orWhere('table_2.company', 'like','%'.$keys.'%');
    })

    ->where('table_1.category', $category)
    ->where('table_1.location', $location)
    ->select('table_2.*', 'table_1.*')
    ->paginate(30);
票数 1
EN

Stack Overflow用户

发布于 2017-02-27 12:25:31

如果您使用的是Laravel约定,那么我希望列名应该是table_1.category_id和table_1.location_id。如果$category和$location是这些记录的整数If,那么它应该按照您期望的方式工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42477487

复制
相关文章

相似问题

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