doumianfeng6979 2016-10-17 14:35
浏览 105
已采纳

如何从具有多个数组条件的laravel中的表中检索数据

Im applying a search filter in a e-commerce website. Here the Customer can choose various fields like color, shape, price, symmetry,polish,clarity and various other things. While the Customer selects any option it should add to the last option he have selected.So he/she may have combination of condition which have to be fetched and shown. For one array condition i can use whereIn in laravel but for multiple array conditions what should I use.Im not sure which array will be empty.

Here is my code for filter :-

public function Filter(Request $request)
{
    $conditions = array();
    if($request->isMethod('post')) {
        $data = array_filter($request->all());

        $shapes = array();
        $amount = array();
        $carat  = array();
        $cut  = array();
        $color = array();
        $clarity = array();
        $cerifying_agency = array();
        $flourscence = array();
        $polish = array();
        $symmetry = array();
       // $conditions=array();
        if(isset($data['shape'])){
            $shapes = $data['shape'];
        }
        if(isset($data['amount'])){
            $amount = $data['amount'];
        }
        if(isset($data['carat'])){
            $carat = $data['carat'];
        }
        if(isset($data['cut'])){
            $cut = $data['cut'];
        }
        if(isset($data['color'])){
            $color = $data['color'];
        }
        if(isset($data['clarity'])){
            $clarity = $data['clarity'];
        }
        if(isset($data['ca'])){
            $cerifying_agency = $data['ca'];
        }
        if(isset($data['fl'])){
            $flourscence = $data['fl'];
        }
        if(isset($data['polish'])){
            $polish = $data['polish'];
        }
        if(isset($data['symmetry'])){
            $symmetry = $data['symmetry'];
        }

    }
}

I have attached the snapshot of the UI and the data format for ajax which is being called in the array of the filter function :-Sorry I couldnt embed the picture as Stackoverflow is not allowing me to do so

  • 写回答

2条回答 默认 最新

  • duanche2007 2016-10-17 15:46
    关注

    You can use function similar to this:

    function appendFilter($query, $array, $fieldName){
        return $query->where(function($query) use ($array, $fieldName) {
            $query->whereIn($fieldName, $array)->orWhereRaw(empty($array) ? "true" : "false");
        });
    }
    

    You can use it like this:

    $productQuery = DB::table("products");
    $productQuery = appendFilter($productQuery, $shapes, "shape");
    $productQuery = appendFilter($productQuery, $otherFilters, "otherField");
    $results = $productQuery->get();
    

    What this function does is it appends nested where with the first part that does WhereIn, but it also has orWhereRaw which appends raw "true" value in case array is empty (false otherwise) So in case your array with filtered values is empty it will not affect the values in your select.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?