Laravel One-to-Many Relationship Association via Form
up vote
0
down vote
favorite
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!
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
add a comment |
up vote
0
down vote
favorite
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!
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
Can't you just pass it anauthor_id
in the url?
– Chin Leung
Nov 19 at 21:29
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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!
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
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!
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
asked Nov 19 at 21:00
DaveInside
11
11
Can't you just pass it anauthor_id
in the url?
– Chin Leung
Nov 19 at 21:29
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06
add a comment |
Can't you just pass it anauthor_id
in the url?
– Chin Leung
Nov 19 at 21:29
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06
Can't you just pass it an
author_id
in the url?– Chin Leung
Nov 19 at 21:29
Can't you just pass it an
author_id
in the url?– Chin Leung
Nov 19 at 21:29
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382548%2flaravel-one-to-many-relationship-association-via-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can't you just pass it an
author_id
in the url?– Chin Leung
Nov 19 at 21:29
Route model binding for sure. your route would be something like: Route::post('newbook/{author}', 'SomeController@create'); author in this case is the id# as the previous poster answered. Then in your controller just type hint: public function someFunction(Author $author)
– Brad Goldsmith
Nov 19 at 22:06