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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比