程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Laravel 选择多个字段返回 null大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Laravel 选择多个字段返回 null?

开发过程中遇到Laravel 选择多个字段返回 null的问题如何解决?下面主要结合日常开发的经验,给出你关于Laravel 选择多个字段返回 null的解决方法建议,希望对你解决Laravel 选择多个字段返回 null有所启发或帮助;

我有这个查询来从 3 个表中检索一些字段值。问题是,如果所选字段之一为空或空,即使其他字段不为空且包含值,查询也会返回空。即使这些字段的某些值为 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]);
}

解决方法

要从主表中获取所有条目,即使第二个表中没有行,您也应该使用 left join:

$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();

大佬总结

以上是大佬教程为你收集整理的Laravel 选择多个字段返回 null全部内容,希望文章能够帮你解决Laravel 选择多个字段返回 null所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。