我需要找出给定团队中的当前球员。我为此创建了一个范围。要求是它检查数据透视表以查看left_at字段是否为null。如果为Null,则表示他们仍在队中(当前球员)。该关系是多态多对多关系。之所以具有多态性,是因为团队也可以让球员和教练一样来去去去。
Team.php
/**
* Scope a query to only include current players.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*/
public function scopeCurrentPlayers($query)
{
return $query->whereHasMorph('players', Player::class, function ($query) {
$query->whereNull('left_at');
});
}
**
* Get all players that have been members of the stable.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphByMany
*/
public function players()
{
return $this->morphedByMany(Player::class, 'member')->using(Member::class)->withPivot(['joined_at', 'left_at']);
}
控制器
$currentTeamPlayers = $team->currentPlayers()->get()->pluck('id');
我希望能从团队中收集到目前的球员,但是我收到以下错误。
+异常:Symfony \ Component \ Debug \ Exception \ FatalThrowableError ^ {#5387 -originalClassName:“ TypeError” #message:“传递给Illuminate \ Database \ Eloquent \ Builder :: getBelongsToRelation()的参数1必须是Illuminate \ Database \ Eloquent \ Relations \ MorphTo的实例,Illuminate \ Database \ Eloquent \ Relations \ MorphToMany的实例,称为in /Users/jeffreydavidson/Projects/Ringside/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php行215“`