I have a controller to get user but the output has relationship where the data is not come from db
Controller:
public function test($id){
$data = User::where("user_group_id",$id)->get();
$tes = $data;
return response()->json([
'message'=>'Data Fetched',
'payload'=>$tes,
],200);
}
Output
"message" : "Data Fetched",
"payload" : [
{
"id":1,
"username":asd,
"profile":{
"age:42"
},
"id":2,
"username":sdf,
"profile":{
"age:60"
},
etc
how can i access the data from the profile to group all the user by its age?
i expect the result like this logic
$data = User::all()->groupBy(profile->age);
thankyou in advance