Select 只有 Laravel 中模型的相关条目
Select only related entries for the model in Laravel
我完全陷入了如何从模型(匹配)中获取两个相关条目(团队)的问题。
问题:
以下代码获得正确匹配,但所有现有团队也是如此。一场比赛只有 两支 支球队相互比赛,而我无法明确获得这两支球队 :-)
Match::with('teams')
->whereBetween('elo', [($request->user()->elo - 100), ($request->user()->elo + 100)])
->where('winner_id', 0)
->where('type', 'normal')
->get();
目标:
我想让这两支球队在比赛和球队创建后为其中一支球队分配一名球员。但如果我的想法是正确的,那么仅仅选择最后两个条目是不够的!
- 创建匹配(检查)
- 创建 2 个团队(勾选)
- 将球员分配到其中一支球队(卡住)
表格:
匹配(id,winner_id,...)
团队 (id, match_id, ...)
玩家 (id, user_id, team_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');
}
}
你能告诉我需要什么吗?
根据你在post的评论中告诉我的内容,一场比赛将有多名球员和多支球队 (2),而且球员和球队都 只有 存在 for/belong 到单个匹配项。考虑到这一点,您正在查看一组相当简单的关系/
匹配模型
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);
}
}
现在要将一名球员分配给其中一支球队(以及比赛),您必须使用 associate 方法。你如何做取决于你开始使用的数据。如果你已经了解这个团队,你可以这样做:
$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);
这将 return 比赛数据以及参加该特定比赛的球队和属于该球队的球员。
我完全陷入了如何从模型(匹配)中获取两个相关条目(团队)的问题。
问题:
以下代码获得正确匹配,但所有现有团队也是如此。一场比赛只有 两支 支球队相互比赛,而我无法明确获得这两支球队 :-)
Match::with('teams')
->whereBetween('elo', [($request->user()->elo - 100), ($request->user()->elo + 100)])
->where('winner_id', 0)
->where('type', 'normal')
->get();
目标:
我想让这两支球队在比赛和球队创建后为其中一支球队分配一名球员。但如果我的想法是正确的,那么仅仅选择最后两个条目是不够的!
- 创建匹配(检查)
- 创建 2 个团队(勾选)
- 将球员分配到其中一支球队(卡住)
表格:
匹配(id,winner_id,...)
团队 (id, match_id, ...)
玩家 (id, user_id, team_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');
}
}
你能告诉我需要什么吗?
根据你在post的评论中告诉我的内容,一场比赛将有多名球员和多支球队 (2),而且球员和球队都 只有 存在 for/belong 到单个匹配项。考虑到这一点,您正在查看一组相当简单的关系/
匹配模型
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);
}
}
现在要将一名球员分配给其中一支球队(以及比赛),您必须使用 associate 方法。你如何做取决于你开始使用的数据。如果你已经了解这个团队,你可以这样做:
$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);
这将 return 比赛数据以及参加该特定比赛的球队和属于该球队的球员。