It is narrated from Sayyiduna Ibn Umar (may Allah be pleased with them both) that the Messenger of Allah (peace and blessings be upon him) said on the occasion of the Battle of Khaybar: "Whoever has eaten from this plant, that is, garlic, should by no means come to the mosque." In the narration of Janab Ubaydullah, it is stated that the Noble Prophet (peace and blessings be upon him) said: "Whoever has eaten from this plant should by no means go near the mosques."
Hadith Referenceصحيح ابن خزيمه / جماع أبواب العذر الذى يجوز فيه ترك إتيان الجماعة / 1661
Hazrat ‘Abbad bin Tamim narrates from his uncle that he stated: The Messenger of Allah (peace be upon him) said: “Whoever eats from this vegetable should not harm us in our mosque with its (odor).”
Hadith Referenceصحيح ابن خزيمه / جماع أبواب العذر الذى يجوز فيه ترك إتيان الجماعة / 1662