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

时间:2019-05-15 08:20:27

标签: php arrays laravel object

我正在得到这样的对象响应:

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

我希望将name和role_id放入设施中,并且希望这样的预期输出

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

                }
            ],

        ]

我如何实现此输出名称,并且role_id在单独的对象中,但我想在一个对象中使用,例如我共享的对象。

我的代码:

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

您的帮助会受到高度赞赏吗?

1 个答案:

答案 0 :(得分:1)

怎么样:

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

无论如何都要同时加入两个表。只需从联接表中选择所有内容。请检查/添加是否还有where子句。