Could I take a Core i7 form a 2012 MBP and put it into a 2018 MBP?
I don't know what exact CPU model numbers are involved here, but a simple check of the sockets for the ranges shows that the 2011 or 2012 i7 will not fit on a motherboard designed for a 2017 or 2018 i5 (of course Apple may have used a 2010 i7 or a 2016 i5 in their computers, but the same thing would apply).

The later i5 also uses a different I/O bus, but that might be backwards compatible (haven't checked as it's irrelevant since you won't be able to fit the CPU in the first place).

Depending on the exact models involved, the old i7 also would require quite a lot more power than the new i5, meaning higher power draw and heat generation, both problems the new computer case and power supply might not be able to handle.