Lionel Messi scored a brilliant hat-trick to sink Leganes and help Barcelona match the all-time La Liga unbeaten record of 38 games.
Luis Suarez should have sent Barcelona ahead but missed from just five yards out after Ousmane Dembele and Messi combined to set him up.
It was soon rendered irrelevant as Messi curled home a sumptuous free-kick in the 27th minute and five minutes later added his second after good work by Philippe Coutinho.
Former Liverpool winger Nabil El Zhar pulled one back for Leganes with a strike across goal which took a small deflection.
But Messi had the last word and deftly flicked the ball past Cuellar late on to seal the victory and his treble.