I’ve got $campus_groups
coming from CampusGroup
$campus_groups = CampusGroup::where('campus_id', $campus_id)->get();
which has
[ {"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"}, {"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"}, {"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"} ]
and $campus_org_groups
coming from CampusOrganizationGroup
$campus_org_groups = CampusOrganizationGroup::where('campus_id', $campus_id)->get();
which has
[ {"id":1,"campus_id":1,"campus_organization_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"}, {"id":2,"campus_id":1,"campus_organization_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"}, {"id":3,"campus_id":1,"campus_organization_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"} ]
and would like to get both together like this (the important key is group_id
and I’m ok if campus_organization_id
is left out)
[ {"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"}, {"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"}, {"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"}, {"id":1,"campus_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"}, {"id":2,"campus_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"}, {"id":3,"campus_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"} ]
If I merge them
$obj_merged = $campus_groups->merge($campus_org_groups);
this is what I’m getting
[ {"id":1,"campus_id":1,"campus_organization_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"}, {"id":2,"campus_id":1,"campus_organization_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"}, {"id":3,"campus_id":1,"campus_organization_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"} ]
If I union them
$obj_merged = $campus_groups->union($campus_org_groups);
this is what I’m getting
[ {"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"}, {"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"}, {"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"} ]
Advertisement
Answer
I’ve changed
$campus_groups = CampusGroup::where('campus_id', $campus_id)->get(); $campus_org_groups = CampusOrganizationGroup::where('campus_id', $campus_id)->get();
to
$campus_groups = collect(CampusGroup::where('campus_id', $campus_id)->get(['id','campus_id','group_id','created_at','updated_at'])); $campus_org_groups = collect(CampusOrganizationGroup::where('campus_id', $campus_id)->get(['id','campus_id','group_id','created_at','updated_at']));
Notice the usage of collect()
and also specification of the fields I really wanted (leaving out campus_organization_id
). Then, the initial merge
$obj_merged = $campus_groups->merge($campus_org_groups);
would return the desired output
[ {"id":1,"campus_id":1,"group_id":1,"created_at":"2021-03-15T08:15:27.000000Z","updated_at":"2021-03-15T08:15:27.000000Z"}, {"id":2,"campus_id":1,"group_id":2,"created_at":"2021-03-15T08:15:37.000000Z","updated_at":"2021-03-15T08:15:37.000000Z"}, {"id":3,"campus_id":1,"group_id":5,"created_at":"2021-03-15T08:16:49.000000Z","updated_at":"2021-03-15T08:16:49.000000Z"}, {"id":1,"campus_id":1,"group_id":3,"created_at":"2021-03-15T08:15:50.000000Z","updated_at":"2021-03-15T08:15:50.000000Z"}, {"id":2,"campus_id":1,"group_id":4,"created_at":"2021-03-15T08:15:55.000000Z","updated_at":"2021-03-15T08:15:55.000000Z"}, {"id":3,"campus_id":1,"group_id":6,"created_at":"2021-03-15T08:17:28.000000Z","updated_at":"2021-03-15T08:17:28.000000Z"} ]