I heard the Messenger of Allah ﷺ say, "Actions are judged only by intentions, and every person will be rewarded according to what they intended. Whoever migrated for the sake of Allah and His Messenger, his migration was for Allah and His Messenger. And whoever migrated to gain some worldly benefit or to marry a woman, his migration was for whatever he migrated toward."
حدثنا أبو بكر بن أبي شيبة حدثنا يزيد بن هارون ح و حدثنا محمد بن رمح أنبأنا الليث بن سعد قالا أنبأنا يحيى بن سعيد أن محمد بن إبراهيم التيمي أخبره أنه سمع علقمة بن وقاص أنه سمع
سمعت رسول الله ﷺ يقول إنما الأعمال بالنيات ولكل امرئ ما نوى فمن كانت هجرته إلى الله وإلى رسوله فهجرته إلى الله وإلى رسوله ومن كانت هجرته لدنيا يصيبها أو امرأة يتزوجها فهجرته إلى ما هاجر إليه
Reference, Bukhari narrated it in Bad al-Wahy, 1/54, and elsewhere, from the hadith of Yahya bin Saeed Ansari and so on, no. 1907, from him, and he alone transmitted it, and Muslim in al-Imarah, chapter on his saying ﷺ "Actions are only by intention"... the hadith of Yazid and Ibn Ramh, from him.