كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
5  
The Book of Mosques and Places of Prayer
(17)
Chapter: Prohibiting one who has eaten garlic, onions, or leeks, and other things that have an offensive odor from coming to the masjid, until that smell has gone away, and such a person should be expelled from the masjid
(17)
باب نَهْىِ مَنْ أَكَلَ ثُومًا أَوْ بَصَلاً أَوْ كُرَّاثًا أَوْ نَحْوَهَا عَنْ حُضُورِ الْمَسْجِدِ ‏‏
Sahih Muslim 561 a

Ibn 'Umar reported:

The Messenger of Allah (ﷺ) said during the battle of Khaybar: He who ate of this plant, i. e. garlic, should not come to the mosques. In the narration of Zubair, there is only a mention of" battle" and not of Khaybar.
حَدَّثَنَا مُحَمَّدُ بْنُ الْمُثَنَّى، وَزُهَيْرُ بْنُ حَرْبٍ، قَالاَ حَدَّثَنَا يَحْيَى، - وَهُوَ الْقَطَّانُ - عَنْ عُبَيْدِ اللَّهِ، قَالَ أَخْبَرَنِي نَافِعٌ، عَنِ ابْنِ عُمَرَ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم قَالَ فِي غَزْوَةِ خَيْبَرَ ‏ "‏ مَنْ أَكَلَ مِنْ هَذِهِ الشَّجَرَةِ - يَعْنِي الثُّومَ - فَلاَ يَأْتِيَنَّ الْمَسَاجِدَ ‏"‏ ‏.‏ قَالَ زُهَيْرٌ فِي غَزْوَةٍ ‏.‏ وَلَمْ يَذْكُرْ خَيْبَرَ ‏.‏
Reference : Sahih Muslim 561a
In-book reference : Book 5, Hadith 86
USC-MSA web (English) reference : Book 4, Hadith 1141
  (deprecated numbering scheme)