There seems to be two different questions present. I will take the second part first. Most of our laws create public sentiment because the Constitution designed it to be this way. The legislative branch, the sector of government in charge of making the laws, consists of individuals whose primary function is to hear the voices of their constituents and carve out laws that reflect this sentiment. For example, recently a Representative in Florida proposed a law to ban people from owning dangerous pets such as Burmese Pythons. He proposed this because the population of Burmese Pythons are astronomically high in his state and his constituents are facing challenges on this end. His law reflects his public's sentiments. People in the state of Nebraska might not share the same sentiment, but the Representative proposes the laws that reflect his public's sentiment.
The first part of your question is more intriguing. Laws are created for political gain are done so for a variety of reasons. The consolidation of personal political power gain might be one reason. Another reason might be out of greed and yet one more might be corruption. There might also be a collusion between political leaders and other elements, such as businesses, and this might be yet another reason why laws are made to protect entrenched interests and enhance political gain.
It is not correct to say that laws are always created for political gains or to reflect public sentiments. Some unscrupulous politicians may be tempted to use the law making process to further their own interests, and in this process may create laws primarily to appeal to public sentiments. But this is not the basic purpose of laws.
The laws are intended to serve the common interest of the people to whom they apply. Such laws when created, may be liked by the people and may strengthen the position of the politicians supporting them. But this is not the main purpose of laws.
One example of a law which neither serves the politicians nor is popular among people, is provided by laws that are designed to regulate smoking.
That is very much what is discussed in Plato’s “Republic” in the form What is justice and What would an ideal state look like?
One point of view is that most ruling classes create and enforce laws for their own benefit, whether they do so consciously or not. Given that modern states generally rule by a system of political parties, this amounts to saying that elected lawgivers create laws that benefit their parties. The German philosopher, Nietzsche, calls this a “master mentality.”
Another point of view is that the non-ruling classes want laws that protect them. They will support those lawgivers who create such laws. The German philosopher, Nietzsche, disagreeably calls this a “slave mentality.”
The French philosopher, Jean-Jacques Rousseau, in his book “The Social Contract” points out that most societies are an amalgam of these two forces. The people need a state to provide protection and services; in return, they are willing to lose some of their personal freedom by submitting to the laws of the state. A society will remain stable only as long as these two forces provide benefits to both political parties and the public.
Marx, of course, said that this happy state of affairs never occurred in nature, because the ruling classes were the employers and had absolute control of their employees. This is why Marx thought that the only outcome that would benefit the people would replace rule by political parties with direct rule by the people.