All societies punish people for stepping outside the bounds of what is acceptable for that society. Without written laws, these punishments can be capricious and vicious.
For example, in India recently, a man committed suicide. According to his mother, he was driven to it because his romantic advances were rejected by a married woman. The mother got a mob of her family and friends together, went and kidnapped the woman and dragged her back to the house where she was raped by several men in punishment. About ten years ago, a child visited a shop and was killed in an accident involving some machinery. The family of the child mobbed the shop and killed three men in retaliation. I don't mean to pick on India, that's just the country where I sometimes read their local news. Similar things happen all the time in the US, though generally at a smaller scale because if a riot gets too big in the US, the police are usually called in to stop it (or it was until 2020, when we saw numerous cases where the Leftist mayor was on the side of the leftist rioters and had the police stand down and let the city burn).
In the absence of written law, police, and courts, riots is how a large society regulates itself. The purpose of the law is to control that tendency of large societies, to give people a feeling that the carrying out of justice is the responsibility of someone else, so they should step back and let officialdom handle it in the official way. The purpose is also to give people a sense of confidence that they know what they can do and what they can't, and what sort of punishments they will face for defying the public will as expressed in the law.
Given that this is the pragmatic purpose of the law, the pragmatic answer is that the laws should reflect the mores and customs of society, and that is pretty much what they do in most societies. Therefore, in Muslim countries, the laws reflect Muslim beliefs, in Christian societies the laws tend to reflect Christian beliefs, and in post-Christian societies, the laws tend to reflect post-Christian beliefs.
So there are two issues: what about multi-cultural societies where there is no widespread consensus on how society ought to be regulated? And are there society-independent moral principles that all laws ought to bow to?
Much of Western Europe and the Anglosphere (pretty much England, the US, Canada, Australia and New Zealand) are now multi-cultural societies because of the presence of so many Christians and post-Christians with often conflicting views. This was generally handled by trying to restrict the laws to just what people could agree on, and allowing liberty in the remainder. For example, homosexuality was made legal although most Christians considered it corrosive of social well-being, and Christians were allowed to teach their kids that homosexuality is wrong although most post-Christians considered that a source of potential violence against homosexuals.
However, this was always a shaky consensus, and whichever side had the power tended to push things in their preferred direction. When Christians had the upper hand, homosexuality was illegal. Now that post-Christians have the upper hand, they are pushing favorable discussions of homosexuality into public grade schools and kindergartens in order to subvert the wishes of the parents. Naturally, this is leading to conflict.
So the pragmatic answer to the question is, in societies that are largely homogeneous, there is little reason for laws to cater to minorities. In societies that are heterogenous there is an argument to be made that laws should be a sort of intersection between the basic moralities of the different groups, allowing liberty in all other cases, but this solution is not stable and conflicts will always arise as the power dynamics in society shift.
The moral answer only applies if you believe in objective morals. And if you believe in objective morals, the answer to any individual question will depend on what you think the objective morals are. If you believe that a fetus is just an organ and that no one except the mother has any interest in the well being of that organ, then the moral position is for the law to make abortion legal. If you believe that a fetus is a human being, and that murder of a human being is wrong, then you believe that the moral position is to make abortion illegal.
So the answer to the objective moral question is that yes, morals should be encoded into laws, but that answer doesn't answer any specific question unless everyone agrees on what the morals are.