Laravel One-to-Many Relationship Association via Form












0















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!










share|improve this question























  • 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
















0















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!










share|improve this question























  • 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














0












0








0








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!










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 21:00









DaveInsideDaveInside

11




11













  • 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



















  • 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

















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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini