Seattle on Monday became the first city in the nation to allow drivers of ride-hailing companies such as Uber and Lyft to unionize over pay and working conditions.

Supporters erupted into cheers after the City Council voted 8-0 in favor of the legislation, which is seen as a test case for the changing 21st century workforce. The companies strongly oppose it, and several council members acknowledged there would be legal challenges ahead but said it was worth doing.

The measure requires companies that hire or contract with drivers of taxis, for-hire transportation companies and app-based ride-hailing services to bargain with their drivers, if a majority shows they want to be represented. Drivers would be represented by nonprofit organizations certified by the city.

The legislation was in keeping with other measures recently passed by the city, including raising the minimum wage to $15 and requiring most employers to provide paid sick leave.

"My intent is to make sure that the people, the drivers, the workers in our community continue to have access to good wage jobs," Councilmember Mike O'Brien said. He added innovation can continue to happen, but it shouldn't be done at the expense of workers.

Many drivers in Seattle are immigrants who depend on full-time work, but some make less than minimum wage and lack basic worker rights, such as sick leave and protection from retaliation, he said.

The National Labor Relations Act does not extend collective bargaining rights to independent contractors.

San Francisco-based Uber and others say federal labor law prevents cities from regulating collective bargaining, and the ordinance would violate federal antitrust laws. Opponents also argue it would be costly for the city to implement, it would violate drivers' privacy since their information would be given to the organization, and it would stifle the growth of the on-demand economy.

In a response to a request for a comment on the legislation, Uber said in a statement Monday it is "creating new opportunities for many people to earn a better living on their own time and their own terms."

San Francisco-based Lyft urged the mayor and council to reconsider the measure and listen to those who seek the flexible economic opportunity the company offers.

"Unfortunately, the ordinance passed today threatens the privacy of drivers, imposes substantial costs on passengers and the city, and conflicts with longstanding federal law," Lyft's statement said.

Charlotte Garden, an assistant law professor at Seattle University, said it's a "virtual certainty" that the ordinance will be challenged in court if it's enacted.

"I anticipate that other cities will consider similar measures, but they may wait to see whether the ordinance survives review by a federal court," she said.

Mayor Ed Murray told the council in a letter Monday he supports the right of workers to unionize but has concerns about the bill. Murray worried about the unknown costs of administering the measure.

Murray said after the vote that he wouldn't sign the legislation but under the City Charter it will still become law.

Uber has about 400,000 drivers nationwide with about 10,000 in Seattle. Its rival, Lyft, also has thousands of drivers in Seattle but declined to give a specific number.

Legal experts have been mixed on how the bill would be challenged in court, including whether the ordinance violates antitrust laws because it would allow drivers to get together and set rates.

Uber is facing a class-action lawsuit in federal court in California over worker classification. The plaintiffs named in the suit say they are Uber employees, not independent contractors, and have been shortchanged on expenses and tips.

Uber and Lyft say drivers have flexibility in deciding when they work and how many hours, and many chose to drive to supplement their income. But drivers don't have a say in rate changes, can be deactivated at will and don't have access to worker protections such as sick leave and minimum wage laws, Dawn Gearhart, a representative with Teamsters Local 117.

Under the proposed ordinance, the city will give certified nonprofit organizations a list of eligible drivers at each company, and the groups must show that a majority of drivers of each company want representation. The organizations would then bargain on behalf of those drivers.

"This is amazing," said Saad Melouchi, 30, who drives for Uber. "I'm so happy for myself and for other drivers."

He said he would like to bargain over safety, living wage and other issues.

Some for-hire drivers who spoke at the public hearing urged the council to hold off on the legislation, saying they didn't have a voice in the process.

The Teamsters Union Local 117 also celebrated the vote, while the National Right To Work Legal Foundation criticized it as a violation of drivers' rights.