mardi 20 avril 2021

How to join Product filter query in Laravel Controller?

   $maincategory=Maincategory::find($id);
   if (isset($_GET['brands'])) {
        $brandarray = implode(",", $_GET['brands']);
        $q->whereIn('products.brand',[$brandarray]);
    }
    if (isset($_GET['discount'])) {
        $from=min($_GET['discount']);
            if(in_array("0",$request->discount) && count($request->discount)==1){
                $to='10';
            }else{
                $to='100';
            }
        $q->whereBetween('products.discount',[$from,$to]);
    }
    if (isset($_GET['sortby'])) {
        if($_GET['sortby']=='discount'){
            $q->orderBy('products.discount', 'DESC');
        }elseif($_GET['sortby']=='hightolow'){
            $q->orderBy('products.actual_price', 'DESC');
        }elseif($_GET['sortby']=='lowtohigh'){
            $q->orderBy('products.actual_price', 'ASC');
        }else{
            $q->orderBy('products.id', 'DESC');
        }
        
    }
    if (isset($_GET['minprice'])) {
        $q->whereBetween('products.actual_price',array($_GET['minprice'],$_GET['maxprice']));
    }
    if (isset($_GET['color'])) {
        $colors = implode(",", $_GET['color']);
        $q->whereIn('products.color',[$colors]);
    }
    
    $productdata= Product::leftjoin('maincategories', 'maincategories.id', '=', 'products.main_category')
    ->leftjoin('secondcategories', 'secondcategories.id', '=', 'products.second_category')
    ->leftjoin('thirdcategories', 'thirdcategories.id', '=', 'products.third_category')
    ->leftjoin('brands', 'brands.id', '=', 'products.brand')->select('products.*', 'maincategories.id as main_category_id', 'maincategories.url as main_category_url', 'maincategories.name as main_category_name', 'secondcategories.id as second_category_id', 'secondcategories.url as second_category_url', 'secondcategories.name as second_category_name', 'thirdcategories.id as third_category_id', 'thirdcategories.url as third_category_url', 'maincategories.name as third_category_name', 'brands.id as brand_id', 'brands.name as brand_name')
    ->where('products.main_category',$maincategory->id)->$q->paginate(6);

This is my method code from the controller. It gives Error: Undefined variable: q How to solve this Error with Laravel Controller? How to join Product filter query in Laravel Controller?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire