lundi 9 juillet 2018

How to create a laravel query from dyanmic filters

I am facing problem to create a Laravel query for dyanmic filters.Everything is dyanmic in filters. My Product filter table is look like blow :- enter image description here

Above is my product_filters table. There is one more table like filters that is below:- enter image description here

Actually My requirement is like below:-

http://localhost:8000/product-category/cushions?material=Cotton~Floral&color=Black

Now see material keyword is having Cotton~Flora and color=Black that i will explode. Now my requirement is i want to fetch products that having material Cotton and color is black and material is floaral and color is black. I want to make combination of query string and fetch the data. Please help me how to resolve this issue. Here is my code:-

if($request->isMethod('get')){
            $data = $request->all();
            if(!empty($data)){
                foreach($data as $key => $values){
                    $explodeFilternames = explode('~',$values);
                    $filterids = DB::table('filters')->select('id')->wherein('filter_name',$explodeFilternames)->get();
                    $getfilterids[$key] = array_flatten(json_decode(json_encode($filterids),true));
                }
            }
        }
        $getproducts = Product::whereIn('category_id',$response['catids'])->where('status',1);
        if(!empty($getfilterids)){
            $filterkeys= array_keys($getfilterids);
            $getallfilterids = array_flatten($getfilterids);
            $getcatProducts = Product::whereIn('category_id',$response['catids'])->select('id')->get();
            $getcatProducts = json_decode(json_encode($getcatProducts),true);
            foreach($getcatProducts as $catproduct){
                $checkProExistsInFilter = ProductFilter::where('product_id',$catproduct['id'])->wherein('filter_type',$filterkeys);
                $checkProExistsInFilter = $checkProExistsInFilter->get(); 
                $checkProExistsInFilter = json_decode(json_encode($checkProExistsInFilter),true);
                echo "<pre>"; print_r($checkProExistsInFilter); die;
            } die;
            $getproductids = array_flatten($getproductids);
            echo "<pre>"; print_r($getproductids); die;
            if(!empty($getproductids)){
                $getproducts = $getproducts->wherein('id',$getproductids);
            }
        }
        $getproducts = $getproducts->get();

Please help me how to resolve this issue. Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire