如何在Laravel的查询中编写if而不中断链和顺序

时间:2019-06-18 15:47:50

标签: laravel laravel-5 eloquent

我有以下查询,如果查询中需要if语句,我该怎么做。以下查询获取基于制造商的产品列表。 如果在此查询之后尝试过,然后使用get();但这对我不起作用

查询:

  $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()

我想要的

        $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()

2 个答案:

答案 0 :(得分:0)

您可以:

$productsFeatured ->whereIn('products.status', ['active']);

if($request->chps_approved ==  '63'){
    $productsFeatured->where(function ($query) {
        $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
    })
}

$productsFeatured->where('users.user_type', 'manufacturer_paid')
    ->leftJoin('products_selected_leeds', 'products.id', '=', 'products_selected_leeds.product_id')
    ->....
    ->get();

可束缚的方法只是返回原始的类实例(在本例中为$ productsFeatured),然后将其用于调用下一个方法。

$class->methodOne()->methodTwo();

相同
$class->methodOne();
$class->methodTwo();

提供了可链接的方法: PHP method chaining?

答案 1 :(得分:0)

您应该使用conditional clauses

使用此方法,您可以编写:

// ...
$productsFeatured ->whereIn('products.status', ['active'])
    ->when($request->chps_approved ==  '63', function ($query) {
        return $query->where('products.low_emitting_material_prerequisite_file', '!=', '');
    })
    ->where('users.user_type', 'manufacturer_paid')
 // continue to chain methods for the query...