PHP-Laravel4.2,忽略->;其中(';table.value';,';=';,$value),如果$value为空
我正在学习Laravel4.2,我对构造函数查询的雄辩性有疑问,我有以下代码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
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');
}
})