Creating a Many-to-many relationship in Laravel with additional data

孤者浪人 提交于 2019-12-11 10:18:05

问题


I have in my database a pivot table that stores extra information. It has 2 foreign keys, and an additional field. Here's what it looks like:

EventTeam
  int event_id (fk)
  int team_id (fk)
  boolean home

The intent here is that an Event may have many teams (in fact, it must have at least 2, but that's not a database constraint), and a team may participate in many events. However, for each event-team relationship, I want to also track whether the team is considered the home team for that event.

How do I define my model with this in mind? Do I have an EventTeam model at all, or do I define a belongsToMany relationship in both the Team and Event models? If I need a separate model, what relationships do I define in it? If I don't, how do I add the boolean field to the pivot table that gets used? I really have no idea how to do this.


回答1:


You dont need a EventTeam model per se, but it could come in handy for seeders or if you are going to attach models to your EventTeam connection anywhere else in your app. This should work:

Event model:

public function teams()
{
    return $this->belongsToMany('Team');
}

Team model:

public function events()
{
    return $this->belongsToMany('Event');
}

For the extra boolean you can use ->withPivot().

$this->belongsToMany('Event')->withPivot('is_home');

See http://laravel.com/docs/eloquent#working-with-pivot-tables for more info.

Updated answers:

1) I would put it in both models so you can access the pivot data from both sides without a problem.

2) It should be to column name indeed.

3) Like i said its not really needed for you in this situation, but you could do this:

EventTeam model:

public function event()
{
    return $this->belongsTo('Event');
}

public function team()
{
    return $this->belongsTo('Team');
}



回答2:


Add withPivot('home') on your relations definitions, then you can access it like this:

$team->events->first()->pivot->home; // 0/1

$event->teams->first()->pivot->home; // 0/1

first is just an example of getting single related model here.

Now, next thing is adding that value to the relation:

$team = Team::find($id);
$event = Event::find($eventId);

$team->events()->attach($event, ['home' => 1]);
// or
$team->events()->attach($eventId, ['home' => 1]);

// or using sync    
$event->teams()->sync([1,5,15], ['home' => 0]);

Another thing is querying that field:

// load first team and related events, that the team hosts
$team = Team::with(['events'=>function ($q) {
  $q->wherePivot('home', 1);
}])->first();

// load only teams that are hosts for any event
$hostTeams = Team::whereHas('events', function ($q) {
  // wherePivot won't work here!
  $q->where('event_team.home', 1);
})->get();

and so on.



来源:https://stackoverflow.com/questions/24959112/creating-a-many-to-many-relationship-in-laravel-with-additional-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!