It is narrated from Sayyiduna Abu Ayyub Ansari (may Allah be pleased with him), he says that the Messenger of Allah (peace and blessings of Allah be upon him) said: "When urinating or defecating, do not face the Qiblah nor turn your back towards it, but rather turn towards the east or the west." Sayyiduna Abu Ayyub Ansari (may Allah be pleased with him) says that when we came to (the land of) Sham, we found toilets built facing the Qiblah. So we would turn away from the Qiblah (while sitting in those toilets) and seek forgiveness from Allah. These are the words of the narration of Abdul Jabbar.
Related hadith on this topic