Lionel Messi scored a hat trick and ensured Barcelona equalled a La Liga record of 38 matches without defeat by beating Leganés 3-1 on Saturday. The champions-elect matched the milestone set by Real Sociedad in 1979-80 by remaining undefeated through 31 matches this season after finishing the last campaign without a loss over the final seven games. The league leaders have not lost in La Liga in a year.

Ernesto Valverde’s side will establish a new unbeaten record if they avoid defeat by Valencia next weekend and would then have another six games left to finish the entire league campaign without defeat. Reaching the mark set by Sociedad underscores the exceptional job Valverde has done in his first year with the team.

Barcelona began the season reeling from the departure of Neymar and having just suffered back-to-back defeats by Real Madrid in the Spanish Super Cup. Eight months later they are marching relentlessly towards the title. This win opened up a 12-point lead over second-placed Atlético Madrid, who face Real in the Madrid derby on Sunday.

Messi scored his seventh free-kick of the season before completing his third league hat-trick of 2017-18. The Argentina forward was fouled outside the area by the Leganés defender Dimitrios Siovas and got up to curl a left-footed shot high beyond the reach of the goalkeeper Iván Cuéllar.

Messi followed his 27th-minute opener with a second goal five minutes later, slotting in a low strike after Philippe Coutinho met his run into the box. Barcelona looked complacent for periods in the second half and paid the price when the Leganés forward Nabil El Zhar struck on the counterattack in the 68th minute, his shot taking a slight deflection off Sergi Roberto on its way past Marc-André ter Stegen.

Messi sealed the victory by flicking his third goal over Cuéllar from close range after receiving a pass from Ousmane Dembélé in the 87th minute. The treble took Messi’s league-leading goal tally to 29.

Roberto, who has already shown his versatility by playing as a midfielder, right-back and forward, added to his repertoire by playing his first match as a left-back for Barcelona, allowing Jordi Alba to rest until he came on as a late substitute along with Andrés Iniesta. Sergio Busquets and Samuel Umtiti were also rested in preparation for the second leg of Barcelona’s Champions League quarter-final against Roma in the Italian capital on Tuesday. Barcelona won the first leg 4-1.