“Now, I have to say this,” Mr. Trump said. “We weren’t friends — I didn’t like him, he didn’t like me, and as we fought and fought and fought, believe it or not, we started to respect each other, then we started to like each other, then we started to love each other, and the fact is, he has been a tremendous supporter.”

“We have to keep him,” the president said of Mr. Heller.

Mr. Heller, the only Senate Republican running in a state that Hillary Clinton carried in the 2016 election, badly needs the help. He is facing a costly and highly competitive race against Representative Jacky Rosen, the Democratic nominee in a state where recent polls indicate that about half the voters do not approve of Mr. Trump.

Mr. Heller was openly critical of Mr. Trump during the 2016 contest, but more recently, he has embraced the president, and during a conference call on Wednesday with White House officials and Nevada Republican leaders and activists, he called him a “great leader.”

At the rally on Thursday, Mr. Heller waxed ecstatic about Mr. Trump, praising him for enacting legislation to help veterans, to steer more money to the military and to cut taxes.

“Mr. President, it’s an honor to work with you in putting Nevada back to work,” Mr. Heller said.

Only hours before the event here, the Trump campaign endorsed Mr. Heller, handing the senator what has become a potent tool for Republicans seeking to rally the party’s core supporters. What is less clear, however, is how it will affect other voters in Nevada, where Mr. Trump’s approval rating is around 40 percent, usually grim territory for members of the president’s party who are seeking re-election.

Ms. Rosen has worked to link Mr. Heller to Mr. Trump, pointing out that the senator has voted with the president 96 percent of the time and arguing that he would be a rubber stamp for Mr. Trump’s agenda should voters return him to Washington.

Mr. Trump spent much of the rally focusing on what he characterized as his successes as president, boasting about the strength of the economy, his attempts to build a southwestern border wall and his Supreme Court nominee, Judge Brett M. Kavanaugh, whose confirmation has hit a snag with a decades-old sexual assault allegation.