dongliang1865 2019-05-15 08:20
浏览 682
已采纳

如何使用laravel将两个对象合并为一个对象

I am getting objects response like this:

                       "facilities": [
            [
                {
                    "facility_id": 1,
                    "speciality_id": null,
                    "is_facility_supervisor": 0,
                    "priv_key": "can_add_doctor,can_view_doctor"
                }
            ],
            {
                "name": "Patient",
                "role_id": 7
            }
        ]

I want name and role_id into facilities and i want expected output like this

               "facilities": [
            [
                {
                    "facility_id": 1,
                    "speciality_id": null,
                    "is_facility_supervisor": 0,
                    "priv_key": "can_add_doctor,can_view_doctor",
                    "name": "Patient",
                     "role_id": 7

                }
            ],

        ]

How i can achieve this output name and role_id is in separate object but i want in one object like i shared.

My Code:

$specialities = DB::table('user_facility')
->select('user_facility.facility_id','user_facility.speciality_id','user_facility.is_facility_supervisor','user_facility.priv_key')
->where('user_id',$currentUser->id)->get();

         $roles = DB::table('roles')
        ->join('user_facility', 'roles.id', 'user_facility.role_id')
        ->where('user_facility.user_id', Auth::user()->id)
        ->select('roles.name','user_facility.role_id')->first();

    $superadmin = $is_super_admin->is_super_admin;
    $specialities = (object) $speciality_id;

    $response = ['is_super_admin' => $superadmin, 'facilities' => array_merge([$specialities,$roles])];

Your help will be highly appreciated?

  • 写回答

1条回答 默认 最新

  • doumicheng6732 2019-05-15 08:29
    关注

    How about:

    $specialitiesAndRoles = DB::table('roles')
    ->join('user_facility', 'roles.id', 'user_facility.role_id')
    ->where('user_facility.user_id', Auth::user()->id)
    ->select('user_facility.facility_id','user_facility.speciality_id','user_facility.is_facility_supervisor','user_facility.priv_key','roles.name','user_facility.role_id')
    ->get();
    

    When you are anyway joining both tables. Just select everything from the joined table. Please check/add if you have any more where clauses.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀