Umar bin Khattab reported,

The Messenger of Allah ﷺ said, "Actions are judged only by intentions, and every person will have only what they intended. Whoever's migration was for the sake of Allah and His Messenger, his migration is to Allah and His Messenger. Whoever's migration was for some worldly gain he sought or a woman he wished to marry, his migration is to what he migrated for."

Muslim 4927

Sahih

حدثنا عبد الله بن مسلمة بن قعنب حدثنا مالك عن يحيى بن سعيد عن محمد بن إبراهيم عن علقمة بن وقاص

عن عمر بن الخطاب قال

قال رسول الله ﷺ إنما الأعمال بالنية وإنما لامرئ ما نوى فمن كانت هجرته إلى الله ورسوله فهجرته إلى الله ورسوله ومن كانت هجرته لدنيا يصيبها أو امرأة يتزوجها فهجرته إلى ما هاجر إليه

مسلم 4927صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Leadership
Chapter
His saying ﷺ, "Actions are judged by intention." And that it includes military expeditions and other deeds
Numbering
Darussalam 4927
Muslim 4927