$credits = User::select('id','username')->where('id', auth()->id())
->with(['game_credits' => function($query) use ($phase){
$query->select('user_id','no_of_game_played','game_credits')
->where('phase_id', $phase->id);
}])->first();
Here I want to return game_credits-no_of_game_played which will give me the remaining credits, How can I do that inside whereHas? Here, USER HAS MANY GAME CREDITS
Maybe instead of using the user model you can call the game credit model and assign whereHas to the user model. Game credit model considered as GameCredit. ex:
Assign user id to variable:
Sum Game Credits:
Sum no of game played:
The Credits:
I hope this will help you. Thanks!