Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:12:21

And the one from Egypt who bought him said to his wife, "Make his residence comfortable. Perhaps he will benefit us, or we will adopt him as a son." And thus, We established Joseph in the land that We might teach him the interpretation of events. And Allah is predominant over His affair, but most of the people do not know.  

And he of Egypt who purchased him, Potifar, the Court Officer (Qitfīr al-‘azīz), said to his wife, Zulaikha (Zulaykhā), ‘Give him an honourable place, ˹an honourable˺ residence with us. Maybe he will be useful to us, or we may adopt him as a son’ — for he was chaste. Thus, just as We delivered him from being killed and from the well, and softened the Court Officer’s heart for him, We established Joseph in the land, the land of Egypt, until he attained what ˹status˺ he attained, that We might teach him the interpretation of events, the interpretation of visions (wa-li-nu‘allimahu min ta’wīli’l-ahādīth is a supplement to an implied ˹missing part˺ connected to makkannā, ‘We established’, in other words, ˹an implied˺ li-numallikahu, ‘so that We might give him kingship’; or else the wāw ˹of wa-li-nu‘allimahu, ‘that We might teach him’˺ is ˹to be understood as being˺ extra). God’s way ˹always˺ prevails, exalted be He, nothing being beyond Him, but most people — and they are the disbelievers — do not know, this.
القرآن:١٢:٢١

وَقَالَ الَّذِي اشْتَرَاهُ مِنْ مِصْرَ لِامْرَأَتِهِ أَكْرِمِي مَثْوَاهُ عَسَىٰ أَنْ يَنْفَعَنَا أَوْ نَتَّخِذَهُ وَلَدًا ۚ وَكَذَٰلِكَ مَكَّنَّا لِيُوسُفَ فِي الْأَرْضِ وَلِنُعَلِّمَهُ مِنْ تَأْوِيلِ الْأَحَادِيثِ ۚ وَاللَّهُ غَالِبٌ عَلَىٰ أَمْرِهِ وَلَٰكِنَّ أَكْثَرَ النَّاسِ لَا يَعْلَمُونَ  

{وقال الذي اشتراه من مصر} وهو قطفير العزيز {لامرأته} زليخا {أكرمي مثواه} مقامه عندنا {عسى أن ينفعنا أو نتخذه ولدا} وكان حصورا {وكذلك} كما نجيناه من القتل والجب وعطفنا عليه قلب العزيز {مكنَّا ليوسف في الأرض} أرض مصر حتى بلغ ما بلغ {ولنعلِّمه من تأويل الأحاديث} تعبير الرؤيا عطف على مقدر متعلق بمكنا أي لنملكه أو الواو زائدة {والله غالب على أمره} تعالى لا يعجزه شيء {ولكن أكثر الناس} وهم الكفار {لا يعلمون} ذلك.