Mental tricks:
Your brain can only handle so much input, so I flood myself with positive feedback, not leaving any room for negative input, both during training and the marathon. For example: don't kick yourself for being under-trained, applaud yourself for being well-rested.
At any given point, do not contemplate how you got there. Figure out the best outcome possible at that point and put a plan in place to make it happen. 'It doesn't matter, it's in the past'.
Somewhat similar to
@tigger536, I try to give myself a mental mathematical reward at every mile. I try to focus on the miles completed, and not what I have left: 5K done, 10K done, into the double digits, half marathon done, into the 20s, etc.
The only mile that matters is the one you are running. The miles behind you are behind you. The XX miles in front of you are too overwhelming to think about. Just finish the mile you are running and then do it again. During a tough marathon, the segments may become smaller. I've finished marathons by making it from street light to street light. Just one more...