(Reuters) - California’s “sanctuary state” laws largely survived a legal attack by the Trump administration on Thursday after a federal judge declined to block state measures aimed at thwarting cooperation with Washington’s crackdown on illegal immigration.

FILE PHOTO: Immigrant supporters protest during the Los Angeles City Council ad hoc committee on immigration meeting to discuss the city's response to threats by the Trump administration to cut funding from Los Angeles and other jurisdictions which federal officials say are providing sanctuary to illegal immigrants arrested for crimes, in Los Angeles, California, U.S., March 30, 2017. REUTERS/Lucy Nicholson

But the judge, John Mendez of U.S. District Court in Sacramento, also rebuked lawmakers, saying that the immigration issue could not ultimately be settled in the courts and said elected leaders needed to put aside differences and forge legislation.

California Governor Jerry Brown, a Democrat, signed into law in October measures that, among other things, prevent police from inquiring about immigration status and curtailing law enforcement cooperation with immigration officers.

The law extended to the entire state protections for illegal immigrants that exist in several so-called sanctuary cities, including Los Angeles and San Francisco.

On Thursday, Mendez largely sided with the state against the Trump administration, which was seeking an injunction to prevent California from enforcing the measures.

Mendez did, however, issue an injunction that bars California officials from imposing fines of up to $10,000 on employers who grant immigration officials access to a private workplace or to employment records.

U.S. Department of Justice spokesman Devin O’Malley called the ruling blocking the fines a “major victory for private employers in California who are no longer prevented from cooperating with legitimate enforcement of our nation’s immigration laws.”

The Department of Justice will continue to fight “unjust policies that threaten public safety,” O’Malley said in a statement.

The judge, in his 60-page ruling, said he hoped his order would “not be viewed through a political lens” and said a long-term immigration solution could not come from “piecemeal opinions issued by the judicial branch.”

Mendez said he was joining “the ever-growing chorus” of judges urging elected officials to set aside polarizing politics and work in a cooperative fashion toward passing immigration legislation. “Our nation deserves it,” wrote the judge, who was appointed by President George W. Bush. “Our Constitution demands it.”

The Justice Department had sued California in March, taking aim at three laws that the administration said violated the U.S. Constitution.

California, the country’s most populous state, leads the nation in its population of illegal immigrants, with more than 2.3 million people lacking legal status, according to the Pew Research Center.

The Trump administration has been battling hundreds of cities and other jurisdictions, many Democratic-governed, that have adopted sanctuary policies.

Thursday’s ruling follows a several legal setbacks for the administration over sanctuary cities. In April, a federal appeals court said the administration cannot deny public safety grants to cities that limit cooperation with the Trump administration, in a case brought by Chicago.