November 2018
Intermediate to advanced
404 pages
10h 16m
English
To create a new item for an instance of your model in the database, call the .save(on:) method:
func newEntry(_ req: Request) throws -> Future<Response> { return try req.content.decode(JournalEntry.self).flatMap { entry in return entry.save(on: req) }.transform(to: req.redirect(to: self.mainPage))}
When the save() method completes, Fluent will automatically have filled in its id with a new, unique value, and it's safe to return.
In order for Fluent to work properly, you're required to remove the default ID in new.leaf:
<input name="id" type="hidden" value="-1" />
Now, let's proceed to implementing the Read operation.
Read now
Unlock full access