In a conference management system: a user can register in conferences but also create conferences. A user that creates a conference has an additional field (description) so its necessary a third table (Conference_Creator).
So there are 3 tables:
Conference: id, name, ....
User: id, name, email,...
Conference_Creator: id, idUser, description
And two relationships:
a one to one identifying relationship between User and Conference_Creator
a one to many non-identifying relationship between Conference_Creator and Conference
Im with some doubts about how to model this context properly using eloquent. For now I have like below:
User Model:
Conference Model:
public function conference_creator(){
return $this->belongsTo('App\User');
}
Conference_Creator Model:
public function conferences(){
return $this->belongsToMany('App\Conference´);
}
Do you know if its correct? And in the case of the user model what is necessary to model the relationship?
Well you already have the relations written out and you can use that same language in code. So something like this should do
// Conference
public function conferenceCreator()
{
return $this->belongsTo(ConferenceCreator::class);
}
// ConferenceCreator
public function user()
{
return $this->belongsTo(User::class);
}
public function conferences()
{
return $this->hasMany(Conference::class);
}
// User
public function conferenceCreator()
{
return $this->hasOne(ConferenceCreator::class);
}
Looking at this structure it sounds to me that you're creating a design mistake. If all users can create a conference why don't you connect the user to the conference and skip the conferenceCreator step. You can then store the conference creator description in the conference table as well. Just my 2 cents. Let me know if I can help you out with that ;)
I would include the description in the conference table, instead of the pivot table. That pivot table conference_creator should only have the user_id and the conference_id. You can then access that pivot table from both the User and Conference models, like so
User Model
// A user can create many conferences
public function conferences(){
return $this->hasMany('App\Conference');
}
// A user can attend many conferences
public function registeredConferences(){
return $this->hasMany('App\ConferenceCreator');
}
Conference Model
// A conference has one creator
public function conferences(){
return $this->belongsTo('App\User');
}
// A conference has many attendees
public function registeredUsers(){
return $this->hasMany('App\ConferenceCreator');
}
Thanks for the answers. But using the 3 tables (user, conference and conference_creator) is incorrect or its just more simplier using just the 2 (user and conference)? And in the conference model the first method shold be "public function conferences ()" or "public function user()"?
If you need to have multiple users attending the same conference, than you need a third table. A conference table can reference only one user_id (which should be the creator) and a user table can reference only one conference_id (which should be the conference this user created). If the user needs to reference multiple conferences, then the conference_user table will do it.
I made a mistake on my previous answer! Here goes the correct version
User Model
// A user can create many conferences
public function conferences(){
return $this->hasMany('App\Conference');
}
// A user can attend many conferences
public function registeredConferences(){
return $this->belongsToMany('App\ConferenceUser', 'conference_id');
}
Conference Model
// A conference has one creator
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
// A conference has many attendees
public function attendees(){
return $this->belongsToMany('App\ConferenceUser', 'user_id');
}