In my Laravel project ,I have users table contain id and username ...etc. another table called predictors ,both of them related in user_id (one to many),now I want to create array contain username from users table and sum of point field in predictors table. I tried many thing but don't work like this :
public function leader()
{
$predects = Predector::selectRaw(' user_id ,sum(point) as points')->groupBy('user_id')->get();
foreach($predects as $predect){
$predect->User->username;
}
return $predects;
}
but it gave me this result
{
"user_id": 1,
"points": "3",
"user": {
"id": 1,
"username": "sarmed",
"nat_id": "123456",
"email_verified_at": null,
"created_at": "2020-11-30T17:10:27.000000Z",
"updated_at": "2020-11-30T17:10:27.000000Z"
}
},
and I want something like this :
{
"user_id": 1,
"points": "3",
"username": "sarmed",
},