php - Laravel 选择多个字段返回 null
问题描述
我有这个查询来从 3 个表中检索一些字段值。问题是如果所选字段之一为空或 null,即使其他字段不为 null 且包含值,查询也会返回 null。即使这些字段的某些值为空或为空,我也想检索所选字段的值。
public function Profile(Request $request)
{
# get profile Data for User
# data from application userID
# data retrive to application username address phone
$profileData = DB::table('users')->where('userId','=',$request['userId'])
->join('patients','users.userId','=','patients.UID')
->join('states','users.stateId','=','states.stateId')
->join('gender','users.genderId','=','gender.genderId')
->select('users.fullName','users.address','users.userPhone','states.stateName',
'patients.hight','patients.weight','patients.bloodGroup'
,'gender.type')
->first();
return response()->json(['data' => $profileData]);
}
解决方案
即使第二个表中没有行,也要从主表中获取所有条目,您应该使用左连接:
$profileData = DB::table('users')->where('userId','=',$request['userId'])
->leftJoin('patients','users.userId','=','patients.UID')
->leftJoin('states','users.stateId','=','states.stateId')
->leftJoin('gender','users.genderId','=','gender.genderId')
->select('users.fullName','users.address','users.userPhone','states.stateName',
'patients.hight','patients.weight','patients.bloodGroup'
,'gender.type')
->first();
推荐阅读
- wordpress - wordpress - 在whatsapp链接中共享私人内容未正确显示
- html - 如何让语义 UI 段正确适应页面宽度
- php - 在 laravel 控制器中进行 Foreach,但只得到单个数据
- machine-learning - 机器学习模型的行业生产参数
- python - Keras model & Tensorflow Estimator - 模型响应处理
- .net - pdf格式在itext7中为空
- c# - 如何通过反射从不同的程序集中调用具有内部委托参数的内部方法?
- mongodb - 如何获取 perl MongoDB 管理模块
- python - 在 Python 中拆分列表并将其插入 Excel
- c++ - 使用示例构建 websocketpp 时出现链接错误