كتاب الْمَسَاجِدِ وَمَوَاضِعِ الصَّلاَةِ
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 566

Abu Sa'id al-Khudri reported:

The Messenger of Allah (ﷺ) along with his Companions happened to pass by a field in which onions were sown. The people stopped there and ate out of that, but some of them did not eat. Then they (Propbet's Companions) went to him. He (first) called those who had not eaten the onions and kept the others (who had taken onions) waiting till its odour vanished.
حَدَّثَنَا هَارُونُ بْنُ سَعِيدٍ الأَيْلِيُّ، وَأَحْمَدُ بْنُ عِيسَى، قَالاَ حَدَّثَنَا ابْنُ وَهْبٍ، أَخْبَرَنِي عَمْرٌو، عَنْ بُكَيْرِ بْنِ الأَشَجِّ، عَنِ ابْنِ خَبَّابٍ، عَنْ أَبِي سَعِيدٍ الْخُدْرِيِّ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم مَرَّ عَلَى زَرَّاعَةِ بَصَلٍ هُوَ وَأَصْحَابُهُ فَنَزَلَ نَاسٌ مِنْهُمْ فَأَكَلُوا مِنْهُ وَلَمْ يَأْكُلْ آخَرُونَ فَرُحْنَا إِلَيْهِ فَدَعَا الَّذِينَ لَمْ يَأْكُلُوا الْبَصَلَ وَأَخَّرَ الآخَرِينَ حَتَّى ذَهَبَ رِيحُهَا ‏.‏
Reference : Sahih Muslim 566
In-book reference : Book 5, Hadith 95
USC-MSA web (English) reference : Book 4, Hadith 1150
  (deprecated numbering scheme)