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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)