I am reading books about Number theory now. It's hard to me.
Seems I use quadratic residues to determine if $x^2 \equiv a\pmod m$, but
I don't know how when m, a not coprime. More specifically, like:
$x^2 \equiv 2\pmod 8$ // is this solvable? I know $x^2 \equiv 1 \pmod 4$ solvable
$x^2 \equiv 6 \pmod 8$ // is this solvable?
If $p$ is a prime such that $p^2\mid a$ and $p^2\mid m$, the you must have $p\mid x$ so $p^2\mid x^2$ so $p^2\mid x^2-a$ only if $p^2\mid a $. In this case, you reduce to the question:
$$y^2\equiv \frac{a}{p^2}\pmod{\frac{m}{p^2}}$$
This solves the cases in your question, since $2^2\mid 8$, but $2^2\not\mid 2$ and $2^2\not\mid 6$, so there cannot be solutions.
If $p\mid a$ and $p\mid m$ but $p^2\not\mid m$, then $p$ is relatively prime to $\frac{m}{p}$, and by Chinese Remainder Theorem, this has a solution if and only if we can solve both:
$$\begin{align}x_1^2&\equiv 0\pmod{p}\\ x_2^2&\equiv a\pmod{\frac mp} \end{align}$$ But the first has a solution, so $x^2\equiv a\pmod{m}$ has a solution in this case if and only if $x^2\equiv a\pmod{\frac mp}$ has a solution.
Repeated use of these two reductions lets you reduce the question to a question with $(a,m)=1$ or conclude the equation has no solution (if some step has a $p^2\mid m$ and $p^2\not\mid a$.)