Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在DB查询LARAVEL中使用函数添加多个select和多个where条件_Php_Mysql_Laravel - Fatal编程技术网

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

Php 在DB查询LARAVEL中使用函数添加多个select和多个where条件,php,mysql,laravel,Php,Mysql,Laravel,我尝试使用一些条件执行一系列查询 public function getFetchFields($masterid,$prodid,$f1,$f2,$f3,$f4,$f5,$f6){ $wholesalerConfig = Session::get('wholesalerDb'); if(isset($wholesalerConfig) && !empty($wholesalerConfig)) {

我尝试使用一些条件执行一系列查询

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();
            }
上面的代码确实工作得很好。但它返回一个空数组

我在GRAMMER.php的第52行收到strtolower错误。由于只有addSelect查询字符串出现了一个小问题,所以问题得到了解决


现在这个查询是定制的,我不知道哪里出错了

你必须重构你的代码,使之更具可读性和可管理性?我想你需要if->else if->else if etc,而不是那么多if条件,如果是这样的话,if($f1!=“”| |$f2!=“”| |$f3!=“”| |$f4!=“”| |$f5!=“”)应该先是if($f1!=“”| | |$f2!=“”| |$f3!=“”)等等。。而且可能&&而不是||