【发布时间】:2021-11-29 02:57:22
【问题描述】:
$loantype = $request['regloan_type'];
$loans = Loan::select('date_release')
->where('invalid',false)
->where('transaction_year', $transyear)
->where('loan_type', $loantype)
->get();
如果变量 $loantype 返回 null 那么我想选择该列中的所有记录。如何在 laravel where 子句中做到这一点?
我使用if else statement 来获得我想要的东西。但是有没有办法在不使用if else statement 的情况下做到这一点?
代码:
$loantype = (empty($request['regloan_type'])) ? null : $request['regloan_type'];
if($loantype==null){
$loans = Loan::select('date_release')
->where('invalid',false)
->where('transaction_year', $transyear)
->get();
}else{
$loans = Loan::select('date_release')
->where('invalid',false)
->where('transaction_year', $transyear)
->where('loan_type', $loantype)
->get();
}
【问题讨论】:
标签: php eloquent laravel-5.8 laravel-query-builder