diff --git a/fluent/relation.md b/fluent/relation.md index 1947767d..56065abc 100644 --- a/fluent/relation.md +++ b/fluent/relation.md @@ -131,7 +131,7 @@ extension Pet { And the opposite for `Toy`. -``` +```swift extension Toy { func pets() throws -> Siblings { return try siblings() @@ -146,6 +146,22 @@ let pet: Pet = ... let toys = pet.toys().all() ``` +To create a new many-to-many relationship you can do the following. + +```swift + +var toy: Toy = ... // Create a new toy +try toy.save() // Save the toy to the db + + +var pet: Pet = ... // Create a new pet +try pet.save() // Save the pet to the db + +// Link them together in the db +var pivot = Pivot(toy, pet) // Create the relationship +try pivot.save() // Save the relationship to the db +``` + ### Preparation To prepare for a relationship with a `Pivot`, simply add the pivot to the `Droplet`'s preparations.