我现在正在处理的是生成报告,我在一页中有多个字段,分别是地址、日期、性别和疫苗
我想要的是即使地址为空并且我输入了性别,它仍然会检索所有已选择的特定性别,如果用户没有在字段上输入任何内容,它将自动检索所有记录。所以我做的是这个
在我的控制器中
if (!$request->input) {
$request->input = "";
}
if (!$request->vaccines) {
$vaccine_id = "";
}else{
$vaccine_id = $request->vaccines;
}
if (!$request->month) {
$newDate = " ";
}else{
$newDate = date("Y-m-d", strtotime($request->month));
}
if (!$request->gender) {
$request->gender = "";
}
$patient = Patient::where('patient_address', 'like' ,'%' . $request->input)->where('patient_sex', 'like' ,'%' . $request->gender)->whereHas('users', function($q) use($vaccine_id){
$q->where('vaccine_id', $vaccine_id);
})->get();
所以我使用LIKE
了运算符,这样即使患者地址为空,它仍然会返回结果,与=
运算符不同,它将自动返回 false 并结束WHERE
子句的其余部分。字符串类型的列就像病人地址和性别一样工作得很好。但我刚刚发现你不能LIKE
在整数上使用运算符。因此,如果我=
在疫苗 ID 上使用,如果条件为假,它不会像LIKE
操作员那样自动检索数据库中的所有数据。
有什么解决办法吗?