Asking about the Battle of Tours (or Poitiers), isn’t God supposed to be on the side of the Muslims so how did they lose?
Victory in battle is dependent on Allah's will.
It is not obligatory that He grant it only to the Muslims, rather His sunnah is that He rotates it, granting it at times to the Muslims and at times to the disbelievers.
Among the reasons that He does the latter include:
- to test the patience of the believers
- to punish them for their sins
- to take martyrs from them and otherwise increase their ranks
- to establish evidence against the disbelievers and hypocrites and in favor of the believers
إن يمسسكم قرح فقد مس القوم قرح مثله وتلك الأيام نداولها بين الناس وليعلم الله الذين آمنوا ويتخذ منكم شهداء والله لا يحب الظالمين وليمحص الله الذين آمنوا ويمحق الكافرين أم حسبتم أن تدخلوا الجنة ولما يعلم الله الذين جاهدوا منكم ويعلم الصابرين
If a wound should touch you - there has already touched the [opposing] people a wound similar to it. And these days [of varying conditions] We alternate among the people so that Allah may make evident those who believe and [may] take to Himself from among you martyrs - and Allah does not like the wrongdoers - And that Allah may purify the believers [through trials] and destroy the disbelievers. Or do you think that you will enter Paradise while Allah has not yet made evident those of you who fight in His cause and made evident those who are steadfast?