dongsheng8664 2016-03-25 08:52
浏览 134
已采纳

在Laravel中构建动态查询 - 如何进行多选项搜索

So I am trying to set up a search page and it has multiple get options But I am curious as to how to set this up correctly, I know this is far from correct as I am doing if statements inside of setting a variable, But I am so lost right now.

Any help would be greatly appreciated.

public function index()
{

    $queryUsername = Request::get('u');
    $queryPostcode = Request::get('p');
    $queryOrderbyPhotos = Request::get('o1');
    $queryOrderbyOnline = Request::get('o2');
    $queryOrderbyTypes = Request::get('o3');

    $users = User::rightJoin('user_profiles','users.id', '=', 'user_profiles.user_id')

    if ($queryUsername) 
    {
        ->where('users.username', '=', "$queryUsername")
    }
    if ($queryPostcode) {   
        ->where('user_profiles.postcode', '=', "$queryPostcode")
    }
    if ($queryOrderbyPhotos) {  
        ->whereNotNull('user_profile.avatar')
    }
    if ($queryOrderbyOnline) {  
        ->orderBy('users.last_online', 'DESC')
    }
    if ($queryOrderbyType) {    
        ->orderBy('users.type', 'DESC')
    }
    ->get();

    return view('view', compact('users'));
}
  • 写回答

2条回答 默认 最新

  • ds2321 2016-03-25 09:19
    关注

    This is how I'll approach the problem. I'll create a variable holding the query builder and then call all the additional query methods on it.

    With Eloquent and actually with any class that allows Method Chaining you can do this:

    $query = User::select(...)->join(..);
    $query->where(...);
    $query->get(...);
    

    So in your case I'll be trying to achieve what you want in this manner:

       public function index()
        {
            $input = Request::all();
    
            $query = User::rightJoin('user_profiles', 'users.id', '=', 'user_profiles.user_id');
    
            if (isset($input['u']) && $input['u'])
                $query->where('users.username', '=', $input['u']);
    
            if (isset($input['p'])  && $input['p'])
                $query->where('user_profiles.postcode', '=', $input ['p']);
    
            if (isset($input['o1']) && $input['o1'])
                $query->whereNotNull('user_profile.avatar');
    
            if (isset($input['o2']) && $input['o2'])
                $query->orderBy('users.last_online', 'DESC');
    
            if (isset($input ['o3']) && $input['o3'])
                $query->orderBy('users.type', 'DESC');
    
            $users = $query->get();
    
            return view('view', compact('users'));
        }
    

    Of course it will be a good idea that you have an additional check for valid input on each input parameter. But this can be achieved in many ways. You can read more about Laravel Controller Validation or Laravel Form Request Validation

    By the way I'll suggest to move all this code in your model or in separate class as I prefer keeping controllers slim.

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

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导