The Messenger of Allah ﷺ forbade these two plants, saying, "Whoever eats them should not come near our mosque." He also said, "If you must eat them, then kill their pungency by cooking them." He meant onions and garlic.
أن رسول الله ﷺ نهى عن هاتين الشجرتين فقال: من أكلهما فلا يقربن مسجدنا وقال: إن كنتم لا بد آكلوهما فأميتوهما طبخا، قال: يعني البصل والثوم.
Abu Dawud (3827), Nasai in Kubra (6646), Tirmidhi in Ilal al-Kabir (2/765), and Bayhaqi in Kubra (3/78) all narrated it through the route of Khalid bin Maysara Attar, from Muawiya bin Qurra, from his father, who related it. Its isnad is hasan because of Khalid bin Maysara, for he is hasan al-hadith. Ibn Adi said, "In my view he is truthful, for I have not seen any munkar hadith from him." Tirmidhi said in Ilal al-Kabir (2/766), "I asked Muhammad about this hadith, and he said, 'It is a hasan hadith.'"On this subject there is also a report from Abu Ziyad Khiyar bin Salama, that he asked Ayesha about onions. She said, "The last food the Messenger of Allah ﷺ ate was food containing onion." This was narrated by Abu Dawud (3829), Nasai in Kubra (6646), and Ahmad (24585), all through the route of Baqiya bin Walid, from Buhayr bin Saad, from Khalid bin Madan, from Abu Ziyad Khiyar bin Salama, who related it. In its isnad is Khiyar bin Salama, whom no one deemed reliable except Ibn Hibban, and Khalid bin Madan alone transmitted from him. For this reason the Hafiz said, "Acceptable," meaning where he is corroborated, but I have not found any corroborator for him. The rest of its narrators are reliable except Baqiya, for he is truthful but practices tadlis, though he did state clearly that he heard it directly, as recorded by Ahmad.As for what was reported from Ali, who said, "Eating cooked garlic was prohibited," this is defective. It was narrated by Abu Dawud (3828) and Tirmidhi (1808), both through the route of Musaddad, who said, Jarra bin Mulayh narrated to us, from Abu Ishaq, from Sharik bin Hanbal, from Ali, who related it. Tirmidhi said, "The isnad of this hadith is not that strong. This has been reported as a saying of Ali himself, and it has also been reported from Sharik bin Hanbal, from the Prophet ﷺ, as a mursal report."I say, the mawquf version of this was recorded by Tirmidhi (1809) from Hannad, who said, Waki narrated to us, from his father, from Abu Ishaq, from Sharik bin Hanbal, from Ali, who said, "Eating garlic is not proper except cooked." As for the mursal version, Abu Hatim Razi pointed to it, as found in Ilal (6/2). He was asked about a hadith narrated by Qays bin Rabi, from Abu Ishaq, from Sharik bin Hanbal, from Ali, from the Prophet ﷺ, who said, "It is not permissible to eat garlic." He said, "This hadith is mistaken. Some say it is from Abu Ishaq, from Sharik bin Hanbal, from Ali, as his own saying, mawquf, and Abdul-Rahman bin Mahdi narrated it from Thawri, from Abu Ishaq, from Sharik bin Hanbal without saying 'from Ali,' 'It is not permissible to eat garlic.' This is more likely correct in my view, because Thawri is the most reliable memorizer among them."I say, this is supported by what Ibn Abu Shayba (24975), Baghawi in Mujam Sahaba (1249), and Abu Naeem in Marifat Sahaba (3735) narrated through routes from Yunus bin Abu Ishaq Subayi, from Umayr bin Qumaymi, from Sharik bin Hanbal, who said, the Messenger of Allah ﷺ said, "Whoever eats this foul plant should not come near our mosque," meaning garlic. Abu Naeem said, "Shuba narrated it likewise from Abu Ishaq."From this it becomes clear that the preserved version is from Sharik bin Hanbal, from the Prophet ﷺ, as a mursal report, for Sharik bin Hanbal did not have companionship with the Prophet.Likewise, what is reported from Uqba bin Amir Juhani is not sound, that the Messenger of Allah ﷺ said to his companions, "Do not eat onions," then said in a low voice, "raw." This was narrated by Ibn Majah (3366) from Harmala bin Yahya, who said, Abdullah bin Wahb narrated to us, Ibn Lahia informed me, from Uthman bin Naeem, from Mughira bin Nahik, from Dukhayn Hajri, that he heard Uqba bin Amir Juhani relate this. Its isnad is daif because of Uthman bin Naeem, who is Ibn Qays Ruayni Misri, and his teacher Mughira bin Nahik Hajri Misri, for both are unknown, as stated in Taqrib.