fair-weather friend
someone who is only your friend when you are happy and successful.
I had a lot of money and I knew a lot of people, but most of them turned out to be fair-weather friends.
Cambridge English Idioms Dictionary