When he began his career in law enforcement, he was the perfect cop — an ex-military man who threw himself into his new job on street patrol for Brooklyn Heights’ 84th Precinct.

Then 10 years ago, Ludwig Paz was hit in quick succession by an ugly divorce and a steep alimony judgment.

This financial hit, his former colleagues told The Post, started him along the path to risky moonlighting — leading, last week, to his being named mastermind in a massive prostitution and gambling racket.

“He started looking for side gigs,” remembered one ex-colleague, who asked not to be identified.

“But he was having trouble,” he recalled of Paz’s 2007 divorce from his wife, Sonia.

“The divorce changed him.”

Paz, 51, of Queens Village, is a retired vice detective who, prosecutors now allege, used his know-how to run brothels in Queens, Brooklyn and Hempstead, LI.

Seven active NYPD cops, including three sergeants, were also arrested — some, prosecutors say, for alerting Paz to upcoming brothel raids.

Paz’s alleged brothel chain included a filthy apartment in Bedford-Stuyvesant, Brooklyn, where services were provided on a stained mattress lit by a red ceiling light.

It’s a far cry from Paz’s beginnings with the NYPD in 1990.

Paz was considered a respectful, “yes, sir” kind of guy, recalled one former supervisor.

Paz, an Ecuadorian of diminutive stature, endured with good humor the ribbing of the 84th Precinct’s fellow Latinos.

“He got picked on because he was short, and he didn’t look like other Latinos from Puerto Rico or Dominican Republic,” said the ex-boss, who asked not to be identified by name.

“They’d say, ‘What, did you work in a pizzeria before this?’ So he developed a quick wit. He gave it right back to them. And on the job he was a quick learner.”

By 2007, Paz and his wife were parents — it is not clear how many kids they had — and living in a ground-floor apartment in Ozone Park, Queens.

On June 15 of that year, Sonia sued her husband for divorce, and court records show it got ugly.

Paz, who represented himself, contested the filing.

Over the next year, records show, they fought over the value of their property, and one party sought a restraining order against the other.

They also fought over “medical child support,” which a Brooklyn judge ordered Paz to pay in a decision finalizing the divorce on April 22, 2008.

Soon Paz’s NYPD paycheck was getting hit with automatic alimony deductions.

“Guys who get divorced” while working for the NYPD, “the city takes money out of their paycheck for support,” said the ex-boss.

Paz started looking for moonlighting gigs — but struggled, at least at first, to find work.

“He was strapped,” he said.

Added another former co-worker, “Paz was always scheming to find a way to make money.”

Paz filed for Chapter 13 bankruptcy in Brooklyn in June 2009, about a year after the divorce came through, records show.

But Paz, who again represented himself, couldn’t even find relief in Bankruptcy Court, records show.

A bankruptcy judge dismissed the filing within three months after Paz failed to file the required paperwork.

Paz retired from the NYPD the next year.

It’s not clear how soon after his retirement that Paz allegedly got started full-time into brothel management.

By the time of his arrest, though, Paz and his new wife, Arelis Peralta, were living high on the hog at his well-kept, white, two-story home in Queens Village.

The couple earned $2 million between August 2016 and September 2017, prosecutors allege.

Peralta and her two adult daughters, who live with the couple, were also arrested in the probe.

The daughters, Jarelis Guzman, 22 and Arisbel Guzman, 20, were released without bail.

Paz and Peralta were being held in lieu of $525,000 bond for him and $400,000 bond for her.

“They had these expensive cars — SUVs and Mercedes — the kids were parking all over the place, blocking driveways,” one neighbor told The Post on Saturday.

The luxury cars were so numerous that “they didn’t have space to park them,” the neighbor said. “People used to complain.”

Some time ago, a white Range Rover was added to the collection.

“It was a ‘wow’ thing, and all the kids stopped to look at it,” one neighbor recalled.

Added another neighbor, “I just assumed he was a car dealer.

“You never know how people come into money,” he added.

Peralta and the daughters dressed in designer clothes and carried designer handbags, neighbors dished.

Peralta “always looks good, designer clothes, designer bags. I’ve seen her in Louis Vuitton” and Gucci, one neighbor said.

They also weren’t very neighborly, she said, adding, “They don’t associate with anybody.”

Additional reporting by Ben Feuerherd and Laura Italiano