我有两个模型Team和Match。
Team
-----------
teamId
teamName
matchId
teamId1 --- belongs to teamId from team
teamId2 --- belongs to teamId from team
关系: =>球队有很多比赛 =>匹配属于团队
我到目前为止所做的是:
匹配模型:
public function teamA()
{
return $this->hasOne('App\Team','teamId','teamA');
}
团队模型:
public function teamB()
{
return $this->belongsTo('App\Match','teamId','teamB');
}
控制器:
Match::with('teamA.teamB')->latest()->get();
输出:
{ "data":[{"matchId":1,"teamA":4,"teamB":5,"winner":4,"points":2,"created_at":null,"updated_at":null,
"team_a":{"teamId":4,"teamName":"team1",
"team_b":null}}]
}
我想要获得的也是teamB名称,但是不知何故为null。请帮忙。