You need common ground outside of work.
I've met people I've had a lot in common with at work and here we are many years later still meet up when the chance arises and chat on and off on whatsapp. One example is a guy who used to juice, into training and likes whores and watches...and we had a very similar dark sense of humour. But I too have made friends that only lasted whilst work lasted...guess it was the strongest link and common ground in our friendship, when that ended the relationship expired too. Find people who are truly similar to you and it will last.
On another note, I find friendships with married men who have young children harder to maintain, those guys have very consuming lives and hardly any space for external distractions, it's virtually impossible for them to make new friends.