It is permissible to eat garlic, onions, leeks, and other plants possessing an unpleasant pungent smell. There is no objection whatsoever in doing so.
There is textual evidence prohibiting those who have eaten garlic, onions, and leeks from attending the mosques, on account of the discomfort that their breath will cause for the angels and for their fellow worshippers.
Ibn `Umar relates that the Prophet (peace be upon him) said: “Whoever eats from this plant (meaning garlic) should not approach our mosque.” [Sahîh al-Bukhârî (853) and Sahîh Muslim (561)]
Anas relates that the Prophet (peace be upon him) said: “Whoever eats from this plant should neither come close to us nor pray with us.” [Sahîh al-Bukhârî (856) and Sahîh Muslim (562)]
Jâbir relates that the Prophet (peace be upon him) said: “Whoever eats garlic or onions should stay away from us” – or he said: “from our mosque” – “and he should remain at his home.” [Sahîh al-Bukhârî (855) and Sahîh Muslim (564)]
These hadîth and others like them prohibit those who have eaten these plants from approaching the mosques where the Muslims pray if bad breath has resulted from doing so. If a person has eaten a small quantity of these plants with his food and it has not resulted in bad breath, then he will not be prevented from attending the congregational prayer.
Eating garlic and onions is a legitimate excuse for not praying in congregation at the mosque. It is the same as the case for a person who finds that his food has been served to him at the time of congregational prayer. Such a person is permitted to stay and eat the food and not attend the congregation.
At the same time, it is not permitted for a person to use garlic as a means to deliberately avoiding having to go to congregational prayer. Whoever eats garlic with this as his underlying intent will be sinful for it. In fact, a Muslim should take some care regarding the time that he eats garlicky food so as to avoid it causing him to miss making his prayers in congregation. Alternatively, he may use some breath fresheners or brush his teeth with toothpaste so that he can still go to the mosque.
And Allah knows best.