I do not know of many jobs that restrict the use of the bathroom, but teaching is definitely one of them. Years ago we had a principal who said we could not leave our students alone for even a moment, including going to the bathroom. We were responsible for the class's safety at all times. When asked what we should do, we were told to call the office, and someone would come to stay with our class when we used the bathroom. That would have worked well except for one thing, the school had no one designated to watch a class. During the next faculty meeting the principal questioned us, asking if we actually thought there was someone walking around the school without an assigned task? She said when we called the office, who did we think was there that could watch our class? When reminded that she told us to call the office, she said she was obviously kidding.
I tried very hard not to use the restroom. Goodbye morning coffee. Goodbye water bottle. Use restroom immediately before school. Hold everything until you had a break, which was sometimes three hours. If all else failed, walk the class down the hall towards the teacher's restroom and have the class sit on the floor, boys on one side and girls on the other. We would play The Quiet Game. If I (Quickly) used the bathroom and didn't hear any noise, the class would get five additional minutes of recess. Most of the time this worked out nicely, especially if there were teachers or paraprofessionals who were kind enough to stand in their doorway, watching their students and keeping an eye on noisy students sitting in the hall.
I can not tell you the best way to handle your class when you need to use the restroom. I have provided a few suggestions. Ask your coworkers what the school policy is, (There won't be one). You can ask other teachers how this is handled in your school.
I do have a suggestion on how to handle students and the restroom. Near the door, have a boy's and girl's lavatory pass. When a student has to go, they take the pass and put it on their desk. Then they use the lav, taking the pass from the top of their desk and hanging it back on it's hook when they return. You may have to set a limit of two times during the school day for students who obviously want an extended break, or go to fool around. For fourth and fifth grade classes, I have put A Destination Sheet near the door. There is room for the date, time out, destination, and time in. A pen attached to a string is nearby. This is helpful if student's misuse the bathroom, and the office wants to know who was out of the room during a certain time.
There must be teachers who have different ideas. Please email them to me at firstname.lastname@example.org.