Umar bin Khattab reported,

I heard the Prophet ﷺ say, "Actions are judged by intentions. Whoever's migration is for worldly gain or to marry a woman, his migration is to what he migrated for. And whoever's migration is to Allah and His Messenger, his migration is to Allah and His Messenger."

Bukhari 3898

Sahih

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

عمر رضي الله عنه قال

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

البخاري 3898صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Virtues of the Ansar
Chapter
Migration of the Prophet ﷺ and his companions to Madinah
Numbering
Darussalam 3898
Bukhari 3898