leader_id === $student->id; } public function courses(): BelongsToMany { return $this->belongsToMany(Course::class, 'study_group_courses'); } public function leader(): BelongsTo { return $this->belongsTo(Student::class, 'leader_id'); } public function members(): HasMany { return $this->hasMany(StudyGroupMember::class); } public function students(): BelongsToMany { return $this->belongsToMany(Student::class, 'study_group_members'); } public static function getBusyStudentIdsForCourses(array $courseIds, ?int $excludeGroupId = null): array { $busyMemberIds = StudyGroupMember::whereHas( 'studyGroup', function ($q) use ($courseIds, $excludeGroupId) { if ($excludeGroupId) { $q->where('study_groups.id', '!=', $excludeGroupId); } $q->whereHas('courses', function ($cq) use ($courseIds) { $cq->whereIn('courses.id', $courseIds); }); } )->pluck('student_id')->toArray(); $busyLeaderIds = static::query() ->when($excludeGroupId, fn ($q) => $q->where('id', '!=', $excludeGroupId)) ->whereHas('courses', function ($cq) use ($courseIds) { $cq->whereIn('courses.id', $courseIds); }) ->pluck('leader_id') ->toArray(); return array_unique(array_filter(array_merge($busyMemberIds, $busyLeaderIds))); } }