shiftOut using SPI isn't working
int latchPin = 12;


You use pin 12 as the latch pin. Once you activate SPI the hardware takes over control of the pins MOSI, MISO and SCK. Pin 12 is MISO and thus attempting to change it in your code will have no effect.

You should use some other pin as the latch pin (pin 10 is often used).




  And shiftOut using SPI is working when I use first code


shiftOut is a software-based method of clocking out bits. It doesn't use hardware and thus it doesn't take control of pin 12.



Note: Answered originally on the Arduino Forum