Relative clauses with prepositions
Put the preposition before the relative pronoun. The preposition determines case; the noun being described determines gender and number. Keep the finite verb at the end.
Understand the idea
A relative pronoun can be the complement of a preposition. The preposition stays directly before the pronoun and determines its case. Gender and number still come from the antecedent, so two independent decisions are involved.
Build it step by step
Find the preposition required inside the relative clause. Determine its case, then choose a pronoun with the antecedent's gender and number. Common dative combinations use mit dem, mit der or mit denen. Accusative combinations include für den, für die and für das. Keep the relative clause's finite verb at the end.
See it in a sentence
Das ist die Kollegin, mit der ich arbeite.
Das ist der Mann, von dem ich erzählt habe.
A useful memory aid
Reconstruct the plain sentence first: I travel with the friend. Then replace the repeated noun phrase by the appropriate preposition plus relative pronoun.
A common trap
Do not leave the preposition at the end as English sometimes does. Also do not contract a relative pronoun with a preposition as if it were an ordinary article.