You should read George MacDonald Fraser's Flashman (1969) and it's sequels, in which the "memoirs" of the fictional schoolboy bully from Tom Brown's schooldays by Thomas Hughes (1857) tell of his involvement in many famous 19th century historical events.
Fraser thus puts Flashman in the middle of real historical events in the real universe and also in the fictional universe of Tom Brown's schooldays by Thomas Hughes. Fraser could do that legally because historic events are not copyrighted and because the copyright of Tom Brown's schooldays (1857) expired before Flashman (1969) was published 112 years later.
It is a good idea to restrict the fictional universes you use as setting for your own fiction to those which are in the public domain.
Works are in the public domain if they are not covered by intellectual property rights, such as copyright, at all, or if the intellectual property rights to the works has expired.1
Every work first published before 1923 has been in the American public domain since 1998. Since January 1, 2019, works from 1923 have also lost their copyright protection2. After that literature, movies and other works released 96 years ago will enter the public domain every January 1 until 2073. From 2073 works by creators who died seven decades earlier will expire each year.3
And most other countries have similarly long copyright terms.
So the fictional universes you can publish stories in without legal problems are limited to those created in the 19th century and earlier and those published early in the 20th century.
I note that the last few novels of famous science Fiction writer Robert A. Heinlein (1907-1988) involved characters traveling through a multiverse that included Heinlein's own fictional settings and some of those of other writers.
In The Cat Who Walks Through Walls (1985), for example:
The Cat Who Walks Through Walls may be regarded as part of Heinlein's multiverse series, or as a sequel to both The Number of the Beast1:145 and The Moon Is a Harsh Mistress. During a meeting of the Council of the Time Scouts, representatives from every major time line and setting written by Heinlein appear, including Glory Road and Starship Troopers, and references are made to other authors' works as well.
I don't know anything about the potential legal problems with Heinlein mentioning the works of other writers, but I suppose that being one of the most famous science fiction writers of all time may have helped getting any permission that Heinlein needed.
I suppose that if you want to write a story about a magical kid going to a school of magic you might want to read not only Harry Potter books but also earlier and less successful and famous books with similar settings.
The Worst Witch is a series of children's books written and illustrated by Jill Murphy. The series are primarily boarding school and fantasy stories, with eight books published. The first, The Worst Witch, was published in 1974 by Allison & Busby,1 and the most recent, First Prize for the Worst Witch, was published in 2018 by Puffin Books, the current publisher of the series. The books have become some of the most successful titles on the Young Puffin paperback list and have sold more than 4 million copies.2
There have also been Worst Witch television shows, etc.
And some of the stories mentioned here would also be useful to a writer of stories similar in some ways to Harry Potter:
And if someone wants to write a story similar in some ways to Star Wars they should remember that the genre of Star Wars could be described as science fiction, science fantasy, space opera, planetary romance, etc., etc. And so they should think of reading classic works in those related genres.
Space opera: https://en.wikipedia.org/wiki/Space_opera4
Planetary Romance: https://en.wikipedia.org/wiki/Planetary_romance5