I'd need to do some research to provide you with citations, so I apologize in advance, but the super-quick answer is that this happened with the transition from largely mercenary and career soldiers to armies of volunteers and conscripts in the late eighteenth and early nineteenth century. The transition is closely related to both the massive increase in the size of armies and the rise of national and ideological conflicts like the French Revolution and Franco-Prussian War. The introduction of volunteers and large conscript armies changed the narrative to one of sacrifice for a cause. I did some research on war memory and commemorative practice, so I'm getting this largely from Fallen Soldiers by George Mosse, a good source but pretty indirect for your question. I hope someone else can answer you more fully.
I imagine that the answer to the question you think you're asking is: nationalism and conscription.
It's interesting, though, to point out the limited extent to which this transition has occurred.
For example, after 'modernising' the Nigerian army engaged in coups for many decades, from a combination of idealism (the modernising soldier) and greed (oil)^1 . The ECOMOG taskforce it led in Liberia was nicknamed Every Car Or Moving Object Gone, because of the amount of theft it engaged in^2 . Many theories of civil war emphasise the extent to which resources (of weapons, ethnic grievances, volunteers, conscripts) fuel military entrepreneurism^3 . Looting opportunities and causes have always gone hand in hand; each makes the other more appealing.
Also look at the massive economic assets of the Chinese and Egyptian militaries. The People's Liberation Army of China was always intensely ideological; this image was developed by propaganda for decades, but don't let that fool you into believing it wasn't genuine. But the PLA also has had fingers in many economic pies. And, although I'm no expert on this subject, I couldn't finish my post without mentioning procurement issues in Western nations. Military expenditure is an excellent vehicle for corruption; it's very hard to know what things should cost, and unless you're in a war with a technological equal, the consequences of mis-spending won't be obvious.
(*I don't think I'm breaking subreddit rules about recent events, trying to make all specifics 20+ yrs old)