如何使用Laravel中的透视表数据查找模型?
我有一个锦标赛和一个俱乐部模型。我使用它们之间的多对多关系。现在我想找到一个使用透视表的俱乐部模型 我试过这个:如何使用Laravel中的透视表数据查找模型?,laravel,pivot,Laravel,Pivot,我有一个锦标赛和一个俱乐部模型。我使用它们之间的多对多关系。现在我想找到一个使用透视表的俱乐部模型 我试过这个: $tournament = Tournament::find(1); $club = $tournament->clubs->wherePivot('team_as_1','1'); return $club; 但它显示:方法illumb\Database\Eloquent\Collection::wherePivot不存在 我的比赛模型: public
$tournament = Tournament::find(1);
$club = $tournament->clubs->wherePivot('team_as_1','1');
return $club;
但它显示:方法illumb\Database\Eloquent\Collection::wherePivot不存在
我的比赛模型:
public function clubs(){
return $this->belongsToMany('App\Club','tbl_club_tournament')->withPivot('team_as_1','team_as_4','team_as_5','team_as_6');
}
我的俱乐部模式:
public function tournament(){
return $this->belongsToMany('App\Tournament','tbl_club_tournament')->withPivot('team_as_1','team_as_4','team_as_5','team_as_6');
}
我想找一家球队1=1的俱乐部。试试看
return $this->hasManyThrough('App\Club', 'App\Tournament');
$tournament = Tournament::find(1);
$club = $tournament->clubs()->wherePivot('team_as_1','1')->get();
return $club;
在当前的方法中,您在集合上调用方法wherePivot(但该方法在集合类中不存在),但是通过调用函数$Tornament->clubs(),该函数返回一个查询生成器对象,您可以在该对象上调用wherePivot()
编辑:
似乎你只需要一件物品,所以你应该这样做
$club = $tournament->clubs()->wherePivot('team_as_1','1')->first();
下面使用
$tournament = Tournament::find(1);
$clubs = $tournament->clubs()->wherePivot('team_as_1','1')->get();
return $clubs;
它不工作..它说:Illumb\Database\Eloquent\Relations\BelongToMany无法转换为字符串