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!
laravel laravel-5 eloquent laravel-5.6 laravel-5.7
add a comment |
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 '18 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 '18 at 22:06
add a comment |
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 '18 at 21:00
DaveInsideDaveInside
11
11
Can't you just pass it anauthor_id
in the url?
– Chin Leung
Nov 19 '18 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 '18 at 22:06
add a comment |
Can't you just pass it anauthor_id
in the url?
– Chin Leung
Nov 19 '18 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 '18 at 22:06
Can't you just pass it an
author_id
in the url?– Chin Leung
Nov 19 '18 at 21:29
Can't you just pass it an
author_id
in the url?– Chin Leung
Nov 19 '18 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 '18 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 '18 at 22:06
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
0
active
oldest
votes
0
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.
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 '18 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 '18 at 22:06