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)
قَالَ رَسُولُ اللهِ ﷺ الْأَعْمَالُ بِالنِّيَّةِ وَإِنَّمَا لِامْرِئٍ مَا نَوَى فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى اللهِ ﷻ وَإِلَى رَسُولِهِ فَهِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ وَمَنْ كَانَتْ هِجْرَتُهُ إِلَى دُنْيَا يُصِيبُهَا أَوِ امْرَأَةٍ يَتَزَوَّجُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ