0

我正在开发一个 Fantasy 体育应用程序。我正在使用的模型是 FantasyPlayer、PlayerGame、TeamGame

FantasyPlayer 可以有很多 PlayerGame 也可以有很多 TeamGame

public function PlayerGame()
{
    return $this->hasMany('App\Models\PlayerGame','player_id','player_id');
}

public function TeamGame()
{
    return $this->hasMany('App\Models\FantasyData\TeamGame','team','fantasy_player_key');
}

当我加载数据时,我目前使用急切加载:

FantasyPlayer::with(['PlayerGame', 'TeamGame'])->take(1)->get();

加载两个关系然后加载哪些关系变得乏味。理想情况下,我想让模型处理这个逻辑。所以我可以做这样的事情:

FantasyPlayer::with(['FantasyGame'])->take(1)->get();

然后我的 FantasyGame 范围将包含我需要基于位置的 FantasyPlayer 值的 PlayerGame 或 TeamGame 记录。像这样的东西是我想要的......但它对我不起作用:

public function scopeFantasyGame($query)
{
    if($this->position == "DEF"){
      return $this->TeamGame();
    }
    else{
      return $this->PlayerGame();
    }
 }

有谁知道我可以使用预先加载并让 FantasyGame 根据 FantasyPlayer 位置属性返回正确关系的方法?:

FantasyPlayer::with(['FantasyGame'])->take(1)->get();
4

1 回答 1

2

#1

您不能根据结果元素有条件地预先加载关系,这是因为预先加载发生在您检索记录之前,这就是为什么这不起作用:

# FantasyPlayer.php

public function scopeFantasyGame($query)
{
    if($this->position == "DEF") // <--- Laravel doens't retrieve the records yet,
    {                            //      so this won't work
      //
    }
    else
    {
      // 
    }
 }

#2

本地查询范围用于约束查询,在您的情况下,您希望加载与此范围的关系,而不是它的一般用途,但确保您可以做到:

# FantasyPlayer.php

public function scopeFantasyGame($query)
{
    return $query->with(['PlayerGame', 'TeamGame']);
}

然后像这样使用它:

# YourController.php

public function myFunction($query)
{
    $fantasyPlayers = FantasyPlayer::fantasyGame()->get();
}

#3

但是,如果你想总是急切地加载关系,为什么要使用查询范围而不是告诉 laravel 默认加载你想要的关系呢?你可以在你的模型中指定它(检查文档本节默认加载):

# FantasyPlayer.php

protected $with = ['PlayerGame', 'TeamGame'];

更新

If you want to retrieve elements that always have a given relationship, you have two paths. For the first one, you could use a query scope to only load those elements:

# FantasyPlayer.php

public function scopeHasFantasyGame($query)
{
    return $query
              ->has('PlayerGame')
              ->has('TeamGame');
}

Then:

# YourController.php

public function myFunction($query)
{
    $fantasyPlayers = FantasyPlayer::hasFantasyGame()->get();
}

The second option would be to retrieve the elements, then filter the collection based on the existence of the relationship (using the Map() function):

# YourController.php

public function myFunction($query)
{
    $fantasyPlayers = FantasyPlayer::all()
                          ->map(function ($fantasyPlayer) {
                              return $fantasyPlayer->PlayerGame()->exists()
                                     && $fantasyPlayer->TeamGame()->exists();
                          });
}
于 2019-04-29T23:59:53.553 回答