Example 4 Find the minimum and maximum values of the function $y=\frac{1+x+x^{2}}{1-x+x^{2}}$.
Solve $y-y x+y x^{2}=1+x+x^{2},(y-1) x^{2}-(y+1) x+(y-1)=0$, this quadratic equation in $x$ has real roots $x$, so the discriminant $\Delta=(y+1)^{2}-4(y-1)^{2}=$ $-(y-3)(3 y-1) \geqslant 0$, from which we can solve $\frac{1}{3} \leqslant y \leqslant 3$. Also, when $x=1$ then $y=3, x=-1$ then $y=\frac{1}{3}$, so $y_{\min }=\frac{1}{3}, y_{\max }=3$.