Abu Saeed Khudri reported,

We had only just conquered Khaybar when we, the companions of the Messenger of Allah ﷺ, came upon that vegetable, garlic, and the people were hungry, so we ate a great deal of it. Then we went off to the mosque, and the Messenger of Allah ﷺ caught the smell and said, "Whoever has eaten any of this foul plant should not come near us in the mosque."The people said, "It has been forbidden, it has been forbidden!"This reached the Prophet ﷺ, so he said, "People, it is not for me to forbid what Allah has made lawful for me, but it is a plant whose smell I dislike."

Muslim 1256

Sahih

و حدثني عمرو الناقد حدثنا إسمعيل ابن علية عن الجريري عن أبي نضرة

عن أبي سعيد قال

لم نعد أن فتحت خيبر فوقعنا أصحاب رسول الله ﷺ في تلك البقلة الثوم والناس جياع فأكلنا منها أكلا شديدا ثم رحنا إلى المسجد فوجد رسول الله ﷺ الريح فقال من أكل من هذه الشجرة الخبيثة شيئا فلا يقربنا في المسجد فقال الناس حرمت حرمت فبلغ ذاك النبي ﷺ فقال أيها الناس إنه ليس بي تحريم ما أحل الله لي ولكنها شجرة أكره ريحها

مسلم 1256صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Mosques and places of prayer
Chapter
Prohibition on those who have eaten garlic, onions, leeks, or similar from attending the masjid
Numbering
Darussalam 1256
Muslim 1256