Laravel many to many polymorphic relationship with an extra relationship on the pivot tablePolymorphic relationships with pivot table in Laravel?OctoberCMS plugin authoring: Problems with has-many relationships - class not foundHow Best to Link to a Polymorphic Relation in LaravelLaravel 5.2 Eloquent - Model through Many-To-Many RelationshipHow to return two one to many relationships in an API response with Laravel controller?Get Nested json array of data Laravel Eloquent model with RelationshipLaravel - Saving to pivot table in many to many relationship with extra columnSort tags by most used in Laravel many to many polymorphicWorking with Laravel Many to Many Polymorphic relationsGet results from pivot table based on the combination of two IDs in Laravel Project
Was this a power play by Daenerys?
How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?
The lexical root of the past tense forms differs from the lexical root of the infinitive form
Ubuntu won't let me edit or delete .vimrc file
Is it a Munchausen Number?
Can you book a one-way ticket to the UK on a visa?
What is the significance of 4200 BCE in context of farming replacing foraging in Europe?
Can the sorting of a list be verified without comparing neighbors?
Should these notes be played as a chord or one after another?
Early arrival in Australia, early hotel check in not available
Control variables and other independent variables
Is there a faster way to calculate Abs[z]^2 numerically?
Two researchers want to work on the same extension to my paper. Who to help?
Adding slope values to attribute table (QGIS 3)
Make all the squares explode
How to make a Lich look like a human without magic in 5e?
Is there enough time to Planar Bind a creature conjured by a one hour duration spell?
Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?
Why do unstable nuclei form?
How could we transfer large amounts of energy sourced in space to Earth?
As programers say: Strive to be lazy
Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019
Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?
Renting a house to a graduate student in my department
Laravel many to many polymorphic relationship with an extra relationship on the pivot table
Polymorphic relationships with pivot table in Laravel?OctoberCMS plugin authoring: Problems with has-many relationships - class not foundHow Best to Link to a Polymorphic Relation in LaravelLaravel 5.2 Eloquent - Model through Many-To-Many RelationshipHow to return two one to many relationships in an API response with Laravel controller?Get Nested json array of data Laravel Eloquent model with RelationshipLaravel - Saving to pivot table in many to many relationship with extra columnSort tags by most used in Laravel many to many polymorphicWorking with Laravel Many to Many Polymorphic relationsGet results from pivot table based on the combination of two IDs in Laravel Project
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a schema like follows:
articles
id - integer
name - string
images
id - integer
name - string
videos
id - integer
name - string
feedback_questions
id - integer
question - string
feedbackables
id - integer
feedback_question_id - integer
feedbackable_id - integer
feedbackable_type - string
feedback_responses
id - integer
feedbackable_id - integer (FK to feedbackables.id)
user_id - integer
response - text
So, many feedback questions (i.e Please rate this on scale of 1-5, Would you recommend this content etc) can be applied to many content types (article, image, video).
This feedback question can then get answered.
I'm having no problems with the many to many setup e.g.
Model Article.php
:
public function feedbackQuestions()
return $this->morphToMany('AppFeedbackQuestion', 'feedbackable');
Model FeedbackQuestion.php
:
...
public function articles()
return $this->morphedByMany('AppArticle', 'feedbackable');
...
However, how can I go about firstly defining the extra column, and then go about using it?
I first thought a One to Many would suffice, but this would need to be specified on the pivot table itself, like
Model Feedbackable.php
:
public function question()
return $this->hasOne('AppFeedbackQuestion', 'id', 'feedback_question_id');
public function answers()
return $this->hasMany('AppFeedbackResponse', 'id', 'feedbackable_id');
Obviously that's not ideal because it makes everything really clunky.
My end goal is to be able to do this.
$articleFeedbacks = Article::find(1)->with('feedbackQuestions.answers');
I have thought about joins in the query builder, could that work? Ideally I'd like to find a relationship-based solution but I'm wondering if it's even possible? My schema is not set in stone so could there be a better way to define that?
Cheers for any help in advance.
laravel
add a comment |
I have a schema like follows:
articles
id - integer
name - string
images
id - integer
name - string
videos
id - integer
name - string
feedback_questions
id - integer
question - string
feedbackables
id - integer
feedback_question_id - integer
feedbackable_id - integer
feedbackable_type - string
feedback_responses
id - integer
feedbackable_id - integer (FK to feedbackables.id)
user_id - integer
response - text
So, many feedback questions (i.e Please rate this on scale of 1-5, Would you recommend this content etc) can be applied to many content types (article, image, video).
This feedback question can then get answered.
I'm having no problems with the many to many setup e.g.
Model Article.php
:
public function feedbackQuestions()
return $this->morphToMany('AppFeedbackQuestion', 'feedbackable');
Model FeedbackQuestion.php
:
...
public function articles()
return $this->morphedByMany('AppArticle', 'feedbackable');
...
However, how can I go about firstly defining the extra column, and then go about using it?
I first thought a One to Many would suffice, but this would need to be specified on the pivot table itself, like
Model Feedbackable.php
:
public function question()
return $this->hasOne('AppFeedbackQuestion', 'id', 'feedback_question_id');
public function answers()
return $this->hasMany('AppFeedbackResponse', 'id', 'feedbackable_id');
Obviously that's not ideal because it makes everything really clunky.
My end goal is to be able to do this.
$articleFeedbacks = Article::find(1)->with('feedbackQuestions.answers');
I have thought about joins in the query builder, could that work? Ideally I'd like to find a relationship-based solution but I'm wondering if it's even possible? My schema is not set in stone so could there be a better way to define that?
Cheers for any help in advance.
laravel
add a comment |
I have a schema like follows:
articles
id - integer
name - string
images
id - integer
name - string
videos
id - integer
name - string
feedback_questions
id - integer
question - string
feedbackables
id - integer
feedback_question_id - integer
feedbackable_id - integer
feedbackable_type - string
feedback_responses
id - integer
feedbackable_id - integer (FK to feedbackables.id)
user_id - integer
response - text
So, many feedback questions (i.e Please rate this on scale of 1-5, Would you recommend this content etc) can be applied to many content types (article, image, video).
This feedback question can then get answered.
I'm having no problems with the many to many setup e.g.
Model Article.php
:
public function feedbackQuestions()
return $this->morphToMany('AppFeedbackQuestion', 'feedbackable');
Model FeedbackQuestion.php
:
...
public function articles()
return $this->morphedByMany('AppArticle', 'feedbackable');
...
However, how can I go about firstly defining the extra column, and then go about using it?
I first thought a One to Many would suffice, but this would need to be specified on the pivot table itself, like
Model Feedbackable.php
:
public function question()
return $this->hasOne('AppFeedbackQuestion', 'id', 'feedback_question_id');
public function answers()
return $this->hasMany('AppFeedbackResponse', 'id', 'feedbackable_id');
Obviously that's not ideal because it makes everything really clunky.
My end goal is to be able to do this.
$articleFeedbacks = Article::find(1)->with('feedbackQuestions.answers');
I have thought about joins in the query builder, could that work? Ideally I'd like to find a relationship-based solution but I'm wondering if it's even possible? My schema is not set in stone so could there be a better way to define that?
Cheers for any help in advance.
laravel
I have a schema like follows:
articles
id - integer
name - string
images
id - integer
name - string
videos
id - integer
name - string
feedback_questions
id - integer
question - string
feedbackables
id - integer
feedback_question_id - integer
feedbackable_id - integer
feedbackable_type - string
feedback_responses
id - integer
feedbackable_id - integer (FK to feedbackables.id)
user_id - integer
response - text
So, many feedback questions (i.e Please rate this on scale of 1-5, Would you recommend this content etc) can be applied to many content types (article, image, video).
This feedback question can then get answered.
I'm having no problems with the many to many setup e.g.
Model Article.php
:
public function feedbackQuestions()
return $this->morphToMany('AppFeedbackQuestion', 'feedbackable');
Model FeedbackQuestion.php
:
...
public function articles()
return $this->morphedByMany('AppArticle', 'feedbackable');
...
However, how can I go about firstly defining the extra column, and then go about using it?
I first thought a One to Many would suffice, but this would need to be specified on the pivot table itself, like
Model Feedbackable.php
:
public function question()
return $this->hasOne('AppFeedbackQuestion', 'id', 'feedback_question_id');
public function answers()
return $this->hasMany('AppFeedbackResponse', 'id', 'feedbackable_id');
Obviously that's not ideal because it makes everything really clunky.
My end goal is to be able to do this.
$articleFeedbacks = Article::find(1)->with('feedbackQuestions.answers');
I have thought about joins in the query builder, could that work? Ideally I'd like to find a relationship-based solution but I'm wondering if it's even possible? My schema is not set in stone so could there be a better way to define that?
Cheers for any help in advance.
laravel
laravel
edited Mar 23 at 11:47
parth kharecha
581411
581411
asked Mar 23 at 10:53
IanIan
510216
510216
add a comment |
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%2f55312951%2flaravel-many-to-many-polymorphic-relationship-with-an-extra-relationship-on-the%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%2f55312951%2flaravel-many-to-many-polymorphic-relationship-with-an-extra-relationship-on-the%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