He was driven out of Spain
To put it mildly, El Cid's relationship with King Alfonso was complex. It all began with King Ferdinand, the father of Alfonso. He was raised at the home of Ferdinand I's eldest son, the future Sancho II of Castile, in the royal court. El Cid served Ferdinand, but after his death, Ferdinand left Sancho the more significant portion of the kingdom and gave Alfonso the smaller portion. El Cid sided with Sancho during the conflict between the brothers. Sancho chose the 22-year-old Cid to be his armiger Regis, or leader of the royal armies when he ascended to the throne of Castile (1065). The young Cid may have already established a reputation for military strength based on his early appointment to a significant position. He participated actively in the talks that led to the creation of al-Muqtadir as a tribune of the Castilian throne in 1067 while traveling with Sancho on a war against the significant Moorish state of Zaragoza (Saragossa).
Then Sancho passed away murdered without having any offspring, leaving Alfonso as the only potential successor. El Cid continued to serve Alfonso after that, but he was no longer considered a friend of the king, and his stature decreased. The new king seems to have made every effort to secure the favor of Sancho's most influential ally. Even though the Cid had now lost his position as armiger Regis to Count Garcia Ordóez (whom Alfonso had grown to hate), and his former influence at court had naturally diminished, he was still permitted to stay there. In July 1074, he married the king's niece Jimena, daughter of the Count of Oviedo. As a result, he joined forces through matrimony with Alfonso's historic royal family. Being forced to wed Alfonso's niece and joining El Cide to the royal line naturally made him furious about it. El Cid was banished from Spain because he disobeyed the king's frequent orders.