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

时间:2016-10-04 16:37:34

标签: php laravel-4 eloquent

我正在学习laravel 4.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变量不为空。

1 个答案:

答案 0 :(得分:2)

在您的calloc()子句中添加子查询:

where