【问题标题】:querying many-to-many-relationship laravel查询多对多关系 laravel
【发布时间】:2015-05-29 16:43:40
【问题描述】:

我与模型 Competition 和 Season 有一个多对多的关系,其中包含一个名为 Competition_season 的数据透视表。

Season->belongsToMany->Competitions->hasMany->Teams->hasMany->Players

我想查询一个球队在特定赛季的所有球员的数据。

类似

\App\Player::with('team.competition.season')->where('team.competition.season.id', 2);

但是,由于赛季是一个集合(比赛有很多赛季),此时我永远无法这样查询它。

谁能指出我正确选择特定赛季球队中所有球员的正确方向?

【问题讨论】:

  • 我相信你应该在最后加上->get()

标签: php laravel-5


【解决方案1】:

首先,您需要在模型中添加关系 hasManyTrough(了解更多信息:has-many-through) 那么,你所需要的就是“whereHas”

$players = \App\Player::with('season')->whereHas('season', function($q){
     $q->where('season.id', '=', 2);
})->get();

【讨论】:

    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2019-02-07
    • 2017-11-03
    • 1970-01-01
    相关资源
    最近更新 更多