lundi 19 novembre 2018

Laravel One-to-Many Relationship Association via Form

Trying to figure out how to save an item that has a OneToMany relationship in this scenario... Let's say for example I have a list of Authors. And each author can have many Books, but a book can have only one Author (I know normally a book can have more than one author, but for this app, they can't).

So I used Artisan and built an Author model and Book model using:

php artisan make:model Author -mcr

php artisan make:model Book -mcr

(so I get a model, migration, and resource controller all in one).

Ok, so I have an "index page" for Authors, which lists all the authors. If I click on an author's name, it links me to a "show page" for that author, and on that show page it says the Author's name and has a list of books s/he's written.

At the bottom of the list of books that this author's written, there is a button that says "Add Another Book".

This is where I get confused... I would like that "Add Another Book" link to bring me to a page with a form to enter details to add said book (title, price, publish date, whatever...). But when I bring up this new book form, how does this form know how to add the proper id in the foreign key field (which would be author_id for the Author it belongs to?

Thanks and looking forward to your guidance and replies!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire