You can constrain eager loading:
$seminars = Seminar::with([ 'meetings.users'])->with(['users' => function ($query) use ($user) {
$query->where('id', $user->id);
}])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I know a User but I need to get Seminars with that User and must use something like this: Seminar::with...
This is what I have:
$user = User::findOrFail($request->user_id);
$seminars = Seminar::with(['users', 'meetings.users'])->whereHas('users', function ($query) use ($user) {
$query->where('users.id', $user->id);
})->get();
users and meetings.users are pivot
Problem that I have is 'I receive all users that participate in seminar that specific users also belongs'
How can I solve this?
This is approach that works:
$seminars = Seminar::whereHas('users',function ($query) use ($user) {
$query->where('users.id', $user->id);
})->whereHas('meetings.users', function ($query) use ($user) {
$query->where('user_id', $user->id);
})->with([
'users' => function ($query) use ($user) {
$query->where('users.id', $user->id);
},
'meetings.users' => function ($query) use ($user) {
$query->where('user_id', $user->id);
}])->get();
Please or to participate in this conversation.