laravel save many to many polymorphic












1















hi i want to save many to many polymorphic



in my case i want to save tag for book-review



it save tag but it save something more than tag



85  AppModelsBook_Review  132 1   2018-11-25 14:42:20 2018-11-25 14:42:20
86 AppModelsBook_Review 132 85
87 AppModelsBook_Review 132 2 2018-11-25 14:42:22 2018-11-25 14:42:22
88 AppModelsBook_Review 132 87
89 AppModelsBook_Review 132 3 2018-11-25 14:42:23 2018-11-25 14:42:23
90 AppModelsBook_Review 132 89


it save the tag and between every tag it save an object of taggable table with tag id of previous taggable object



this is my tag model relation



public function books_review()
{
return $this->morphedByMany(Book_Review::class, 'taggable');
}


and this is my book-review model relation



public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}


and this the part that i save tag in my controller



    foreach ($request->tagid as $item) {


$book_review_tag = new Taggable();
$taggable_id = strval($book_review->id);
$book_review_tag->taggable_type = $this->Model;
$book_review_tag->taggable_id = $taggable_id;

$book_review_tag->tag_id = $item;
$book_review->tags()->saveMany([$book_review_tag]);

}


i use debugger and show me that it get id as a parameter when it goes back to the first of the for loop
and yet i don't know how to fix it










share|improve this question

























  • i can't get why it saves id

    – Mahdi
    Nov 25 '18 at 16:55


















1















hi i want to save many to many polymorphic



in my case i want to save tag for book-review



it save tag but it save something more than tag



85  AppModelsBook_Review  132 1   2018-11-25 14:42:20 2018-11-25 14:42:20
86 AppModelsBook_Review 132 85
87 AppModelsBook_Review 132 2 2018-11-25 14:42:22 2018-11-25 14:42:22
88 AppModelsBook_Review 132 87
89 AppModelsBook_Review 132 3 2018-11-25 14:42:23 2018-11-25 14:42:23
90 AppModelsBook_Review 132 89


it save the tag and between every tag it save an object of taggable table with tag id of previous taggable object



this is my tag model relation



public function books_review()
{
return $this->morphedByMany(Book_Review::class, 'taggable');
}


and this is my book-review model relation



public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}


and this the part that i save tag in my controller



    foreach ($request->tagid as $item) {


$book_review_tag = new Taggable();
$taggable_id = strval($book_review->id);
$book_review_tag->taggable_type = $this->Model;
$book_review_tag->taggable_id = $taggable_id;

$book_review_tag->tag_id = $item;
$book_review->tags()->saveMany([$book_review_tag]);

}


i use debugger and show me that it get id as a parameter when it goes back to the first of the for loop
and yet i don't know how to fix it










share|improve this question

























  • i can't get why it saves id

    – Mahdi
    Nov 25 '18 at 16:55
















1












1








1








hi i want to save many to many polymorphic



in my case i want to save tag for book-review



it save tag but it save something more than tag



85  AppModelsBook_Review  132 1   2018-11-25 14:42:20 2018-11-25 14:42:20
86 AppModelsBook_Review 132 85
87 AppModelsBook_Review 132 2 2018-11-25 14:42:22 2018-11-25 14:42:22
88 AppModelsBook_Review 132 87
89 AppModelsBook_Review 132 3 2018-11-25 14:42:23 2018-11-25 14:42:23
90 AppModelsBook_Review 132 89


it save the tag and between every tag it save an object of taggable table with tag id of previous taggable object



this is my tag model relation



public function books_review()
{
return $this->morphedByMany(Book_Review::class, 'taggable');
}


and this is my book-review model relation



public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}


and this the part that i save tag in my controller



    foreach ($request->tagid as $item) {


$book_review_tag = new Taggable();
$taggable_id = strval($book_review->id);
$book_review_tag->taggable_type = $this->Model;
$book_review_tag->taggable_id = $taggable_id;

$book_review_tag->tag_id = $item;
$book_review->tags()->saveMany([$book_review_tag]);

}


i use debugger and show me that it get id as a parameter when it goes back to the first of the for loop
and yet i don't know how to fix it










share|improve this question
















hi i want to save many to many polymorphic



in my case i want to save tag for book-review



it save tag but it save something more than tag



85  AppModelsBook_Review  132 1   2018-11-25 14:42:20 2018-11-25 14:42:20
86 AppModelsBook_Review 132 85
87 AppModelsBook_Review 132 2 2018-11-25 14:42:22 2018-11-25 14:42:22
88 AppModelsBook_Review 132 87
89 AppModelsBook_Review 132 3 2018-11-25 14:42:23 2018-11-25 14:42:23
90 AppModelsBook_Review 132 89


it save the tag and between every tag it save an object of taggable table with tag id of previous taggable object



this is my tag model relation



public function books_review()
{
return $this->morphedByMany(Book_Review::class, 'taggable');
}


and this is my book-review model relation



public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}


and this the part that i save tag in my controller



    foreach ($request->tagid as $item) {


$book_review_tag = new Taggable();
$taggable_id = strval($book_review->id);
$book_review_tag->taggable_type = $this->Model;
$book_review_tag->taggable_id = $taggable_id;

$book_review_tag->tag_id = $item;
$book_review->tags()->saveMany([$book_review_tag]);

}


i use debugger and show me that it get id as a parameter when it goes back to the first of the for loop
and yet i don't know how to fix it







laravel many-to-many






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 17:05







Mahdi

















asked Nov 25 '18 at 11:22









MahdiMahdi

156




156













  • i can't get why it saves id

    – Mahdi
    Nov 25 '18 at 16:55





















  • i can't get why it saves id

    – Mahdi
    Nov 25 '18 at 16:55



















i can't get why it saves id

– Mahdi
Nov 25 '18 at 16:55







i can't get why it saves id

– Mahdi
Nov 25 '18 at 16:55














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%2f53466949%2flaravel-save-many-to-many-polymorphic%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%2f53466949%2flaravel-save-many-to-many-polymorphic%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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python