I heard the Messenger of Allah ﷺ say: ʿDeeds are but by intentions and each man will have but that which he intended. If a man's migration was for the sake of Allah, then his migration was for that for which he migrated, but if his migration was to achieve some worldly aim or to take some woman in marriage, his migration was for that for which he migrated.ʿ (Using translation from Aḥmad 168)
سَمِعْتُ رَسُولَ اللهِ ﷺ يَقُولُ إِنَّمَا الْأَعْمَالُ بِالنِّيَّاتِ وَإِنَّمَا لِامْرِئٍ مَا نَوَى فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ فَهِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ وَمَنْ كَانَتْ هِجْرَتُهُ لِدُنْيَا يُصِيبُهَا أَوِ امْرَأَةٍ يَتَزَوَّجُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ