mardi 18 juin 2019

How can I write if in query in laravel without breaking the chain and sequence

I have the following query i need if statement in the query how can i do that. the following query gets the list of products based on manufacturers. If tried if after this query and then use get(); but that not work for me

Query:

  $productsFeatured = Product::select('products.low_emitting_material_prerequisite_file',
        'manufacturers.phone','manufacturers.address','manufacturers.linkedin_url','manufacturers.city', 
        'products.specs_file','products.specs_file_url','products.low_emitting_material_credit_file', 
        'products.id', 'products.name', 'products.logo', 'products.manufacturer_id',
        'products.division_id', 'products.section_id', 'products.website', 'divisions.name as division_name', 'divisions.code as division_code',
        'sections.name as section_name', 'sections.code'   
         )
                ->whereIn('products.status', ['active'])
                ->where(function ($query) {
                    $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
                })
                ->where('users.user_type', 'manufacturer_paid')
                ->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
                ->leftJoin('sections', 'products.section_id', '=', 'sections.id')
                ->leftJoin('divisions', 'products.division_id', '=', 'divisions.id')
                ->join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')
                ->join('users', 'manufacturers.user_id', '=', 'users.id')
                ->orderBy('divisions.code', 'asc')
                ->orderBy('sections.code', 'asc')
                ->groupBy('products.id');
                ->get()

What I want

        $productsFeatured = Product::select('products.low_emitting_material_prerequisite_file',
        'manufacturers.phone','manufacturers.address','manufacturers.linkedin_url','manufacturers.city', 
        'products.specs_file','products.specs_file_url','products.low_emitting_material_credit_file', 
        'products.id', 'products.name', 'products.logo', 'products.manufacturer_id',
        'products.division_id', 'products.section_id', 'products.website', 'divisions.name as division_name', 'divisions.code as division_code',
        'sections.name as section_name', 'sections.code'   
         )
         $productsFeatured ->whereIn('products.status', ['active'])
                if($request->chps_approved ==  '63'){
                    $productsFeatured->where(function ($query) {
                        $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
                    })
                }

                ->where('users.user_type', 'manufacturer_paid')
                ->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
                ->leftJoin('sections', 'products.section_id', '=', 'sections.id')
                ->leftJoin('divisions', 'products.division_id', '=', 'divisions.id')
                ->join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')
                ->join('users', 'manufacturers.user_id', '=', 'users.id')
                ->orderBy('divisions.code', 'asc')
                ->orderBy('sections.code', 'asc')
                ->groupBy('products.id');
                 ->get()





via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire