فَاسْتَجَابَ لَهُ رَبُّهُ فَصَرَفَ عَنْهُ كَيْدَهُنَّ ۚ إِنَّهُ هُوَ السَّمِيعُ الْعَلِيمُ (34)
(12:34) His Lord granted his prayer and warded off their guile from him. *29 Indeed, He hears everyone and knows everything.
*29) Allah warded off their guile from Prophet Joseph by strengthening his character in such a way as to make ineffective all their devices to ensnare him, This also implies that Allah opened the door of prison for him in order to keep him safe from their tricks and temptations.