Alfonso's eldest son, Ferdinand, died in 1275 at the Battle of Écija against the Moroccan and Granadan invasion armies, leaving two infant sons. Alfonso's second son, Sancho, claimed to be the new heir, in preference to the children of Ferdinand de la Cerda, basing his claim on an old Castilian custom, that of proximity of blood and agnatic seniority. Alfonso preferred to leave the throne to his grandsons, but Sancho had the support of the nobility. A bitter civil war broke out resulting in Alfonso's being forced in 1282 to accept Sancho as his heir instead of his young grandsons; only the cities of Seville, Murcia and Badajoz remained faithful to him. Son and nobles alike supported the Moors when he tried to unite the nation in a crusade; and when he allied himself with Abu Yusuf Yakub, the ruling Marinid Sultan of Morocco, they denounced him as an enemy of the faith. A reaction in his favor was beginning in his later days, but he died defeated and deserted at Seville in 1284, leaving a will, by which he endeavored to exclude Sancho, and a heritage of civil war.