dshkmamau65777662 2014-11-17 12:41
浏览 161
已采纳

Laravel:高级搜索表单查询

I have an advanced search form to filter out results from a database using Laravel. The data is filtered correctly but I have a requirement for the user to be able to filter by first name or last name using the same text box (in the advanced form). I tried orWhere to make sure it filters the name field with the first name or last name but the orWhere doesn't consider the other filters. The code I am using is as follows:

DB::table('mytable')
                ->where(function($query) use ($name, $degree_s, $specialty_s, $city_s, $state_s, $lundbeck_id_s) {

                if ($name)
                    $query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%"); # this is whats causing the issue
                if ($specialty_s)
                    $query->where('primary_specialty', $specialty_s);
                if ($city_s)
                    $query->where('city', $city_s);
                if ($state_s)
                    $query->where('state_province', $state_s);
                if ($lundbeck_id_s)
                    $query->where('customer_master_id', $lundbeck_id_s);
                if ($degree_s)
                    $query->where('primary_degree', $degree_s);
                })

                ->select('id', 'first_name','last_name')

Adding the orWhere clause causes the query to not use the other conditions as well (like city_s or state_s).

  • 写回答

2条回答 默认 最新

  • dtye7921 2014-11-17 12:54
    关注

    You need to change:

    if ($name)
       $query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");
    

    into:

    if ($name) {
       $query->where(function($q) use ($name) {
             $q->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");
       });
    }
    

    to make Laravel to add parentheses so it will work as you expect.

    EDIT

    Of course you don't need to wrap everything with closure here, so the best solution for that would be:

    <?php
    
    $query = DB::table('mytable')->select('id', 'first_name', 'last_name');
    
    if ($name) {
        $query->where(function ($q) use ($name) {
            $q->where('first_name', 'like', "$name%")
                ->orWhere('last_name', 'like', "$name%");
        });
    }
    if ($specialty_s) {
        $query->where('primary_specialty', $specialty_s);
    }
    if ($city_s) {
        $query->where('city', $city_s);
    }
    if ($state_s) {
        $query->where('state_province', $state_s);
    }
    if ($lundbeck_id_s) {
        $query->where('customer_master_id', $lundbeck_id_s);
    }
    if ($degree_s) {
        $query->where('primary_degree', $degree_s);
    }
    
    $data = $query->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛