我正在尝试使用 Laravel 控制器中的嵌套 foreach 循环,从长期的雄辩关系中构建一个用户 ID 数组,但无法使其正常工作。
用户可以有发布者,这些发布者可以有团队,每个团队都有成员。用户可以在多个团队中,因此我还需要删除重复的 ID。
我想以一个计数结束,看看有多少团队成员与用户相关联。
在我的用户模型中
public function publishers()
{
return $this->belongsToMany('App\Publisher')->withTimestamps();
}
在我的发布者模型中
public function teams()
{
return $this->belongsToMany('App\Team')->withTimestamps();
}
在我的团队模型中
public function members()
{
return $this->belongsToMany('App\User')->withPivot('status', 'title', 'team_role_ids')->withTimestamps();
}
在我的配置文件控制器中
foreach ($user->publishers as $userPublisher) {
foreach ($userPublisher->teams as $publisherTeam) {
$teamUserIds[] = $publisherTeam->members->pluck('id')->toarray();
}
}
$deDupedIds = array_unique($teamUserIds, SORT_NUMERIC);
$idsCount = count($deDupedIds);
但是我得到了多个数组,而不仅仅是一个已编译的数组,而且计数不起作用。知道我做错了什么吗?
慕仙森
www说