I have one to many relation based two tables users
and games
and there is also bridge table users_games
(linking user_id
to games).
I want to fetch a single record from games table based on provided game_id for specific user. I did some research and found whereHas()
which is returning all games which are belongs to specific user. But I need to fetch one based on game_id
. Can some one kindly let me know how can I fix issue in below script
$GameInfo = User::with('games')->whereHas('games', function ($query) use($request)
{
$query->where('game_id', '=', $request->game_id);
})->find(request()->user()->id);
Is this what you're trying to do?