1
在我的Laravel應用程序中,我有用戶,朋友和羣組的概念。用戶可以創建並將朋友分組。從Laravel的收藏中排除項目
用戶可以有很多朋友:
function friendsOfMine()
{
return $this->belongsToMany('App\User', 'helpers', 'user_id', 'helper_id')
->wherePivot('accepted', '=', 1);
}
一個組可以有很多用戶:
public function groupMembers()
{
return $this->belongstoMany('App\User')->withTimestamps();
}
在UI添加朋友一組,我想說明的完整列表一個用戶的朋友,但排除那些已經添加到該組的朋友。我的組控制器功能看起來像這樣,但我是積極的,我是非基地。
public function add($id)
{
$group = Group::findOrFail($id);
$helpers = $group->groupMembers;
$id = $helpers->lists('id');
$invitees = $this->user
->friendsOfMine()
->where('helper_id', '!=', $id)
->Paginate(5);
return view('groups.add', compact('group','helpers','invitees'));
}
理想情況下,我很樂意爲一些方法來寫:
$helper = $group->friendsOfMine->not->groupMembers->Paginate(5);
反正是有使用來自兩個不同型號的功能過濾數據?
工作非常好 - 感謝您的快速反應,並增加了細節。查詢範圍正是我所需要的。 –