YHWH was with Joseph, and he became a successful man, and he was in the house of his Egyptian master.
Genesis 39:2

Joseph was a successful man and a slave at the same time. God was with him throughout all his troubles, so why did God allow him to be enslaved? God always has a plan. Sometimes bad things happen because we make foolish choices. Sometimes it's because God needs to move us into position for his plans. Don't let your circumstances fool you into thinking God is done with you or has abandoned you.