The use of the military inside the US is controversial because of the 1878 Posse Comitatus Act.
However, the law does not say “the military may not be used for law enforcement.” It says “the military may not be used for law enforcement, except in cases and under circumstances expressly authorized by the Constitution or Act of Congress.”
Trump is counting on the fact that protecting our borders is constitutional.
https://www.oann.com/white-house-grants-law-enforcement-powers-use-of-force-to-troops-at-border/