doubi8383 2015-08-03 06:07
浏览 38

laravel 5搜索查询问题

I need to search filtering different columns from DB table, those filtering columns are groupA, groupb and groupC, each column has one of following values high, low, moderate in db table

But front end user have anther value called "none", but that value is not contain in database.

This is my current query

  public function show($groupA, $groupB, $groupC)
    {
        $FodMaps = FodMap::where('groupA', '=', $groupA)->where('groupB', '=', $groupB)->where('groupC', '=', $groupC)->get();
        return $FodMaps;


    }

What I want is if user search with value which does not contain in db (high/low/moderate) it should search the other columns except the one which have unmatching values

ex: http://url/api/fodmap/groupA=low/groupB=low/groupC=high

if user enter above way it should display the results according to the values

but if user inputs like following

http://url/api/fodmap/groupA=low/groupB=low/groupC=none

since "none" value in groupC column does not exit it should search only other two columns without considering the value of groupC

please advice

  • 写回答

1条回答 默认 最新

  • doubao6464 2015-08-03 08:00
    关注

    You can add query condition dynamically like this.

    
    
        public function show($groupA, $groupB, $groupC)
        {
            $query = FodMap::query();
            if ($groupA !== 'none') {
              $query->where('groupA', '=', $groupA)
            }
    
            if ($groupB !== 'none') {
              $query->where('groupB', '=', $groupB)
            }
    
            if ($groupC !== 'none') {
              $query->where('groupC', '=', $groupC)
            }
    
            return $query->get();
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大