The Prophet ﷺ said during the expedition of Khaybar, "Whoever has eaten from this plant, meaning garlic, must not come near our mosque."
أن النبي ﷺ قال في غزوة خيبر: من أكل من هذه الشجرة- يعني الثوم- فلا يقربن مسجدنا.
Bukhari narrated it in the Book of Prayer (853), and Muslim in the Book of Mosques (561:68), both of them via Yahya Qattan, from Ubaydullah, who said, "Nafi told me," from Ibn Umar, and he mentioned it.