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:52

That is so al-'Azeez will know that I did not betray him in ˹his˺ absence and that Allah does not guide the plan of betrayers.  

‘That, plea of innocence, is so that he, the Court officer, may know I did not betray him, with regard to his family, in his absence (bi’l-ghayb is a circumstantial qualifier), and that truly God does not guide ˹to success˺ the guile of the treacherous; he ˹Joseph˺ then humbled himself before God and said:
القرآن:١٢:٥٢

ذَٰلِكَ لِيَعْلَمَ أَنِّي لَمْ أَخُنْهُ بِالْغَيْبِ وَأَنَّ اللَّهَ لَا يَهْدِي كَيْدَ الْخَائِنِينَ  

{ذلك} أي طلب البراءة {ليعلم} العزيز {أني لم أخنه} في أهله {بالغيب} حال {وأن الله لا يهدي كيد الخائنين} ثم تواضع لله فقال.