in Education by
Hi I am having problems with relation and database design. There are three tables called 'articles' , 'users' , and 'companies' with one attribute each called id: "articles" -id "users" -id "companies" -id The article table contain many publishers that are user model or company model. My pivot table must be something similar to 'authors' -id, -model_id, -model_name, -article_id Is there any way to get article's publisher ids and data from models in one collection with a single query? Maybe I misunderstood the problem and there is a simpler solution for that case. thx in advance JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Assuming Article only have one company or one user , The right way to do that is to do a one to many (polymorphic) relationship creating the table with these columns:- { article_id , articleable_id , articleable_type } and then define it in your model use Illuminate\Database\Eloquent\Model; class Article extends Model { /** * Get all of the owning articleable models. */ public function articleable() { return $this->morphTo(); } } and the Users table:- class Userextends Model { /** * Get all of the user's articles. */ public function articles() { return $this->morphMany('App\Article', 'articleable'); } } and same in the Company model:- class Company extends Model { /** * Get all of the companies articles. */ public function articles() { return $this->morphMany('App\Article', 'articleable'); } } and then you can retrieve the relationship using:- $user = App\User::find(1); foreach ($user->articles as $article) { // do this } as mentioned here but if the article can have more than user or company then you have to do many to many polymorphic as mentioned here

Related questions

0 votes
    I have following Comments-on-Models design (source: http://railscasts.com/episodes/154-polymorphic-association): ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    In the code below I pivot a dataframe using an index date. After the pivot, I need to get the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Here, the context of polymorphic is expecting 'Derived' from 'Base&. Given class P { }; class Q : ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Suggestions needed for creating better and efficient search indexes for models having foreign key and many-to-many ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    How many components are present in generalized linear models? (a) 2 (b) 4 (c) 6 (d) None ... programming questions and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
0 votes
    How many types of relationships available in Laravel Eloquent?...
asked Sep 30, 2021 in Technology by JackTerrance
0 votes
    I have a blade template that creates a form. The form uses a foreach loop to iterate through a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have a blade template that creates a form. The form uses a foreach loop to iterate through a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    So I've seen a few posts describing this, but can't quite figure it out for my case. I used to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    I am using Python 2.6.6 and SQLAlchemy 0.6.6 to handle a one to many relationship in my database ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    Say I have a Game object with many Player objects (a baseball game). The lineup for each game has ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    A table on the many side of a one to many or many to many relationship must: (a) Be in Second ... is from Normal Forms topic in chapter Normalization of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Science that deals with the relationship of various organisms with their environment is known as Select the correct answer from above options...
asked Dec 30, 2021 in Education by JackTerrance
0 votes
    If a two port network is passive, then we have, with the usual notation, the relationship as _________ (a) h21 ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 13, 2021 in Education by JackTerrance
...