在DB查询LARAVEL中使用函数添加多个select和multiple where条件

时间:2016-12-21 05:37:38

标签: php mysql laravel

我正在尝试使用某些条件执行一系列查询。

public function getFetchFields($masterid,$prodid,$f1,$f2,$f3,$f4,$f5,$f6){
          $wholesalerConfig = Session::get('wholesalerDb');
          if(isset($wholesalerConfig) && !empty($wholesalerConfig))
          {
              $wholesalerdet = DB::table('user')->where('ID',$wholesalerConfig)->first();
              $wholesalerdb = $wholesalerdet->DBName;
              \Config::set('database.connections.wholesaler.database', $wholesalerdet->DBName);
          }

          $getting_fields_3 = DB::connection('wholesaler')->table('product')
          ->join('stock','stock.RefProduct','=','product.ID')
          ->select('stock.AvailableStock as AvailableStock','product.ID as productID')
          ->where('product.MasterID',$masterid)->where(function($addwhere)use($f1,$f2,$f3,$f4,$f5){
            if($f1 != "Null" || $f1  != ""){
              $addwhere->where('product.Field1','=',$f1);
            }
            if($f1 != "" || $f2 != ""){
              $addwhere->where('product.Field1',$f1)->where('product.Field2',$f2);
            }
            if($f1 != "" || $f2 != "" || $f3 != ""){
            $addwhere->where('product.Field1',$f1)->where('product.Field2',$f2)->where('product.Field3',$f3);
            }
            if($f1 != "" ||  $f2 != "" ||  $f3 != "" ||  $f4 != ""){
            $addwhere->where('product.Field1',$f1)->where('product.Field2',$f2)->where('product.Field3',$f3)->where('product.Field4',$f4);
            }
            if($f1 != "" ||  $f2 != "" ||  $f3 != "" ||  $f4 != "" || $f5 != ""){
              $addwhere->where('product.Field1',$f1)->where('product.Field2',$f2)->where('product.Field3',$f3)->where('product.Field4',$f4)->where('product.Field5',$f5);
            }
        });

            if($f1 != "Null" || $f1  != ""){
                $selects = $getting_fields_3->addSelect('product.Field2','product.Field3')->get();
              }
            if($f1 != "" || $f2 != ""){
              $selects = $getting_fields_3->addSelect('product.Field3','product.Field4')->get();
            }
            if($f1 != "" || $f2 != "" || $f3 != ""){
              $selects = $getting_fields_3->addSelect('product.Field4','product.Field5')->get();
            }
            if($f1 != "" ||  $f2 != "" ||  $f3 != "" ||  $f4 != ""){
              $selects = $getting_fields_3->addSelect('product.Field5','product.Field6')->get();
            }
            if($f1 != "" || $f2 != "" || $f3 != "" || $f4 != "" || $f5 != ""){
              $selects = $getting_fields_3->addSelect('product.Field6')->get();
            }

上面的代码确实很有效。但它返回一个空数组。

我在第52行的GRAMMER.php中收到了strtolower错误。由于只有addSelect查询字符串存在一个小问题,因此解决了。

现在这个查询是自定义构建的,我不知道我哪里出错了。

0 个答案:

没有答案