仅为Laravel中的模型选择相关条目
我完全被困在如何从模型(比赛)中只获取两个相关条目(团队)中 问题:仅为Laravel中的模型选择相关条目,laravel,laravel-5,eloquent,query-builder,laravel-5.2,Laravel,Laravel 5,Eloquent,Query Builder,Laravel 5.2,我完全被困在如何从模型(比赛)中只获取两个相关条目(团队)中 问题: class Match extends Model { protected $table = 'matches'; public function teams() { return $this->hasMany(Team::class); } public function winner() { return $this->belo
class Match extends Model
{
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
下面的代码得到了正确的匹配,但是所有现有的团队也得到了正确的匹配。一场比赛只有两支球队相互对抗,而这两支球队我无法明确说明:——)
目标:
class Match extends Model
{
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
我想让这两支球队在比赛和球队成立后,为其中一支球队分配一名球员。但是如果我的想法是正确的,那么仅仅选择最后两个条目是不够的
class Match extends Model
{
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
比赛(id、获胜者\u id等)球队(id,比赛id,…)
玩家(id、用户id、团队id,…) 关系:
class Match extends Model
{
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
你能告诉我这需要什么吗?根据你在这篇文章的评论中告诉我的,一场比赛将有多个球员和多支球队(2),球员和球队都只为一场比赛而存在。考虑到这一点,您将看到一组相当直接的关系/ 匹配型号
class Match extends Model {
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
class Team extends Model {
protected $table = 'teams';
public function match()
{
return $this->belongsTo(Match::class);
}
public function players()
{
return $this->hasMany(Player::class);
}
}
class Player extends Model {
protected $table = 'players';
public function team()
{
return $this->belongsTo(Team::class);
}
}
团队模式
class Match extends Model {
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
class Team extends Model {
protected $table = 'teams';
public function match()
{
return $this->belongsTo(Match::class);
}
public function players()
{
return $this->hasMany(Player::class);
}
}
class Player extends Model {
protected $table = 'players';
public function team()
{
return $this->belongsTo(Team::class);
}
}
播放器型号
class Match extends Model {
protected $table = 'matches';
public function teams()
{
return $this->hasMany(Team::class);
}
public function winner()
{
return $this->belongsTo(Team::class, 'winner_id');
}
}
class Team extends Model {
protected $table = 'teams';
public function match()
{
return $this->belongsTo(Match::class);
}
public function players()
{
return $this->hasMany(Player::class);
}
}
class Player extends Model {
protected $table = 'players';
public function team()
{
return $this->belongsTo(Team::class);
}
}
现在,要将一名球员分配给其中一支球队(以及比赛),必须使用以下方法。你如何做到这一点取决于你从什么数据开始。如果您已经了解团队,您可以这样做:
$team = Team::find(123);
$player = Player::find(8734); //could also have created new Player here
$team->players()->associate($player);
如果你不了解球队,但只知道比赛,你可以这样做:
$match = Match::with('teams')->find(9003);
$team = $match->teams[0]; //choose teams[1] for second team, etc.
$player = Player::find(8734); //could also have created new Player here
$team->players()->associate($player);
您始终可以通过以下方式获取所有相关数据:
Match::with('teams.players')->find(9003);
这将返回比赛数据以及参加该特定比赛的球队和该球队的球员。请用代码解释问题所在。你在关系上有问题吗?我有问题用雄辩的语言指出正确的查询,以便将两支球队分配到一场比赛中。好的,那么两支球队的标准是什么?我的意思是你选择两支球队的依据是什么?只需加上where('match_id',此处为你的_VALUE_),它就会将你分配到球队,问题是什么。我忘了解释这些“球队”和足球队不一样。每支球队总是由5名得分相同的球员组成。所以一个队只能参加一场比赛。这会影响您上次的数据查询吗?因为它看起来很漂亮,而不是我的:-)所以你是说一支球队只参加过一场比赛?那么一名球员可以参加多支球队吗?不。这更像是一个瑜伽团体,每次都会改变。你知道一个“moba游戏”吗?在这个游戏中,球员可以参加多支球队的比赛。但是一名球员可以参加多场比赛,对吗?还是每场比赛只有一名球员和一支球队,而每场比赛他们都是全新的?在我修改我的答案之前,只需要把关系弄清楚。每一场比赛都是全新的。