Except the layoffs come from someone's spreadsheet 3 levels up, so even if everyone likes you that may not be enough. It definitely helps, but not guaranteed.
The point isn’t about saving yourself from the layoff, it’s about having a network to help you find a new job, because yeah, the layoffs are disconnected, heartless, and clumsy. If you’re always building at least a couple relationships, some of those people inevitably quit on their own, branch out, and after years of this, you end up with a decently sized network to help you out (and you can help them too when the time comes)