The Prophet ﷺ said during the expedition of Khaybar, "Whoever has eaten from this plant, meaning garlic, must not come near our mosque."
أن النبي ﷺ قال في غزوة خيبر: من أكل من هذه الشجرة- يعني الثوم- فلا يقربن مسجدنا.
Bukhari reported it in the Book of Adhan (853), and Muslim in the Book of Mosques (561), both from the hadith of Yahya, from Ubaydullah, who said, Nafi narrated to me, from Ibn Umar, and he mentioned it. Muslim also reported it through another route, from Abdullah bin Numayr, from Ubaydullah, and in it, "Whoever eats from this plant should not come near our mosques until its smell has gone."