tehseen
5 months ago

Get product by filter specific category

Posted 5 months ago by tehseen

I have a category single page where i get products by category slug (single category page), also i have relationship between category and product so i get all product by category which is fine (below listed).

$category = Category::where('slug', '=', $slug)->firstOrFail();

$categoryProducts = $category->products()->paginate(24);

//all product by above category 
dd($categoryProducts);

So what i try to do is i want to filter those product via category like what i did in product listing.

 $products = Product::where(function($query){

            $all = Input::has('all') ?  Input::get('all') : null;
            $free = Input::has('price') ?  Input::get('price') : null;
            $popular = Input::has('popular') ? Input::get('popular') : null;
            $paid = Input::has('paid') ? Input::get('paid') : null;

            if(isset($free)){
                $query-> where('price','<=', '0');
            }

            if(isset($popular)){
                $query-> where('popular','=', $popular);
            }

            if(isset($paid)){
                $query-> where('price','>', '0');
            }

        })->paginate(24);

any idea how to achieve this ?

Please sign in or create an account to participate in this conversation.