Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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-Laravel4.2,忽略->;其中(';table.value';,';=';,$value),如果$value为空_Php_Laravel 4_Eloquent - Fatal编程技术网

PHP-Laravel4.2,忽略->;其中(';table.value';,';=';,$value),如果$value为空

PHP-Laravel4.2,忽略->;其中(';table.value';,';=';,$value),如果$value为空,php,laravel-4,eloquent,Php,Laravel 4,Eloquent,我正在学习Laravel4.2,我对构造函数查询的雄辩性有疑问,我有以下代码 public static function filterInvoicingReport($valuesFilter){ $fechaInicio=''; $fechaFin=''; $customerId=''; $state=''; if($valuesFilter == null) return; //obtenemos los valores

我正在学习Laravel4.2,我对构造函数查询的雄辩性有疑问,我有以下代码

public static function filterInvoicingReport($valuesFilter){

    $fechaInicio='';
    $fechaFin='';
    $customerId='';
    $state='';
    if($valuesFilter == null)
        return;

    //obtenemos los valores de las variables dateRegisterStart y dateRegisterEnd
    if(array_key_exists('fechaInicio', $valuesFilter)&&array_key_exists('fechaFin', $valuesFilter)){
        $fechaInicio=$valuesFilter['fechaInicio'];
        $fechaFin=$valuesFilter['fechaFin'];
    }
    if(array_key_exists('customerId',$valuesFilter)){
        $customerId=$valuesFilter['customerId'];
    }
    if(array_key_exists('state',$valuesFilter)){
        $state=$valuesFilter['state'];
    }



    $sql= DB::table('invoice')
        -> select(
            'invoice.id_invoice',
            'invoice.invoice_number',
            'invoice.created_date',
            'invoice.name_seller',
            'invoice.conditions',
            'invoice.total_sale',
            'invoice.status',
            'invoice.customerId',
            'customer.name as customerName',
            'customer.identificationId as customerDNI')
        -> whereBetween(DB::raw('DATE_FORMAT(invoice.created_date,"%Y-%m-%d")'),[$fechaInicio,$fechaFin])
        ->Where('invoice.status','=',$state)
        ->Where('invoice.customerId','=',$customerId)
        -> join('customer','customer.customerId','=','invoice.customerId');
        -> get();
    return $sql;
}
是对报表的查询,问题是
$state
$customerId
可以为空,并且查询返回所有空


我想要的是,如果php中的变量为空,则只返回我的
->where
变量不为空。

将子查询添加到
where
子句:

    ->Where( function ($query) use ($state){ 
        if(!empty($state)) {
            $query->where('invoice.status','=',$state);
        } else {
            $query->whereNotNull('invoice.status');
        }
    })
    ->Where( function ($query) use ($customerId){ 
        if(!empty($customerId)) {
            $query->where('invoice.customerId','=',$customerId);
        } else {
            $query->whereNotNull('invoice.customerId');
        }
    })

将子查询添加到
where
子句:

    ->Where( function ($query) use ($state){ 
        if(!empty($state)) {
            $query->where('invoice.status','=',$state);
        } else {
            $query->whereNotNull('invoice.status');
        }
    })
    ->Where( function ($query) use ($customerId){ 
        if(!empty($customerId)) {
            $query->where('invoice.customerId','=',$customerId);
        } else {
            $query->whereNotNull('invoice.customerId');
        }
    })