dqwh1201 2016-01-19 20:59
浏览 23
已采纳

需要找到另一种在Laravel / PHP中查询搜索结果的方法

I am having a really tough time figuring out another approach to do search querys on my site. At the moment i have search critierias for "Min size, Max size, City, street, Min. price, Max. price.

So i have these queries (build using Laravel standard):

if (!empty($by) && empty($adresse) && empty($minstor) && empty($maxstor)) 
        {
            $users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('by', '=', $by)->orderBy('created_at', 'DESC')->get();
        }
        elseif (!empty($by) && !empty($adresse) && empty($minstor) && empty($maxstor)) 
        {
            $users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('by', '=', $by)->where('adresse', '=', $adresse)->orderBy('created_at', 'DESC')->get();
        }
        elseif (empty($by) && !empty($adresse) empty($minstor) && empty($maxstor)) 
        {
            $users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->where('adresse', '=', $adresse)->orderBy('created_at', 'DESC')->get();
        }
        else
        {
            $users = User::where('pris', '<', $maxpris)->where('pris', '>', $minpris)->orderBy('created_at', 'DESC')->get();
        }

All of this simply for the user being able to search for the for the critirias above, but if i want to add several more feautures, i can just imagine how many different if functions i will need to run in order to get all the specific inputs etc. It will evovle eksponentially really quick.

How can i get around this problem without having to create hundreds of different if else statements in order to return the correct query?

  • 写回答

2条回答 默认 最新

  • doulu1907 2016-01-19 21:38
    关注

    You could set up an array of your filters with their corresponding operator. Then iterate over the filters that were passed and running the them (This hasn't been tested but should get you headed in the right direction):

    $available_queries = [
        ['input' => 'maxpris', 'operator' => '<', 'field' => 'pris'],
        ['input' => 'minpris', 'operator' => '>', 'field' => 'pris'],
        ['input' => 'by',      'operator' => '=', 'field' => 'by'],
        ['input' => 'adresse', 'operator' => '=', 'field' => 'addresse'],
    ]
    
    $query = User::orderBy('created_at', 'DESC');
    
    foreach($available_queries as $filter){
        if(request()->input($filter['input'])){
            $query = $query->where(
                $filter['field'],
                $filter['operator'],
                request()->input($filter['input'])
            );
        }
    }
    
    $users = $query->get();
    

    EDIT: I found an error in my original script. Updated to fix the error and make more readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题