iPhone charging from MacBook Air 2017
The issue is that it starts to charge for like 1 second, disconnects for 5 seconds, and then loops like that.

Dirt or another containment in the port isn't going to cause a loop that cycles in a regular intervals.   When you connect an iPhone to a MacBook, there is a charging negotiation - normally, the USB spec is for 5V @ .5A, but the MacBook can &quot;boost&quot; the available charge up to 2.5A - it has to &quot;ask&quot; for it, however.  That &quot;loop&quot; that you're seeing is a connection, an ask and a disconnect and then the device trying again.
It could be a couple of things...

You have an issue with your port not being able to supply enough power to the phone
You don't have sufficient power on your USB bus
There could be a hardware issue - either with the MacBook or the iPhone

If you can charge your phone correctly otherwise (i.e. with a regular USB power adapter) and your USB port functions normally, start with an SMC Reset.  If that doesn't fix it, try Safe Mode by holding ⇧ Shift while booting.  But, to be candid, I don't see that having much effect, if any at all.  Charging is not handled by the OS, but by the firmware on the SMC chip.  In this case, ensuring your firmware and software is fully updated would be the next best thing.
Lastly, you want to ensure that your connectivity is good - avoid cheap, knock off cables and connectors.  I assisted someone just yesterday who insisted against my advice on using one of those 5-way USB spider things that has all the different USB connectors on one cable she picked up at a trade show;  It fried her Dell XPS' charging ports.  Use good quality cables from trusted brands (I like Anker personally). Saving $5-10 on a cable simply isn't worth it.
If it is the hardware (either phone or Mac) you might just want to avoid charging with your Mac and stick to the wall adapter.  To fix it would require replacing logic board(s) and it may not be worth the expense.  If it's under AppleCare...well, that's a different story altogether.  You'll have to make the call to see if it's worth the AppleCare incident for this issue.