如何过滤laravel 5中的数据?

时间:2016-01-03 07:23:07

标签: laravel laravel-5

查看

...
<span class="opensans size13"><b>Gender</b></span>
                <select class="form-control" name="gender" id="gender" placeholder="Gender">
                  <option value="" selected="">Gender</option>                      
                  <option value="M">Male</option>
                  <option value="F">Female</option>                       
                </select>   
            <span class="opensans size8" style="color: red;"></span>                                                    
            <div class="clearfix pbottom15"></div>

            <span class="opensans size13"><b>Published</b></span>
                <select class="form-control" name="published" id="published" placeholder="Published">
                  <option value="" selected="">Published</option>                       
                  <option value="YES">YES</option>
                  <option value="NO">NO</option>                          
                </select>   
            <span class="opensans size8" style="color: red;"></span>    
...

<div class="col-md-5">
                <div class="right wh70percent">
                    <select class="form-control" name="status_sort" id="status_sort" placeholder="Status">
                      <option value="" selected="">Status</option>                      
                      <option value="N">N</option>
                      <option value="BL">BL</option>                          
                    </select>
                </div>
            </div>
...     

服务

public function dataCustomerList($param) 
    {
        $status = $param['status_sort'];
        $gender = $param['gender'];
        $published = $param['published'];

        if($published=='NO'){
            $published_check = 'NO';
            $published = 0;
        }
        else if($published=='YES'){
            $published_check = 'YES';
            $published = 1;
        }

        if(empty($status)) 
            $customer = customer::paginate(10);
        else
            $customer = customer::where('tb_customer.customer_status', '=', $status)
                                ->paginate(10);

        if(!empty($gender) AND !empty($published_check)){
            $customer = customer::where('tb_customer.gender', '=', $gender)
                                ->where('tb_customer.published', '=', $published)
                                ->paginate(10);
        }
        else if(!empty($gender)){
            $customer = customer::where('tb_customer.gender', '=', $gender)
                                ->paginate(10);
        }
        else if(!empty($published_check)){
            $customer = customer::where('tb_customer.published', '=', $published)
                                ->paginate(10);
        }
        return $customer;  
    }   

界面如下:http://imgur.com/AoggrZz

当我选择已发布和性别,然后按搜索按钮时,数据将显示

当我只选择状态时,显示的数据符合

但是当我选择性别,已发布和状态时,显示的数据不匹配

我想再问一次。除了我的代码,还有其他更简单的代码吗?

谢谢

1 个答案:

答案 0 :(得分:2)

每次添加过滤器时都应构建查询,例如:

    $customer = customer;

    if(!empty($status)) {
        $customer = $customer->where('tb_customer.customer_status', '=', $status);
    }

    if (!empty($gender)) {
        $customer = $customer->where('tb_customer.gender', '=', $gender);
    }

    if (!empty($published_check)) {
        $customer = $customer->where('tb_customer.published', '=', $published);
    }

    return $customer->paginate(10);

通过这种方式你只需添加条件,你就不需要检查每一个案例并再次写相同的查询

免责声明:我不是Laravel用户,您可能需要稍微编辑代码,只是说出构建查询的方式