【问题标题】:Method to get all items with `where` and pivot table[Laravel 5]使用`where`和数据透视表获取所有项目的方法[Laravel 5]
【发布时间】:2015-08-09 13:05:56
【问题描述】:
我需要获取所有 project_group.admin_id' 等于当前用户 ID 的项目。
这个find 方法将只返回一项:
$projects = Group::find(1) -> projects()->where('project_group.admin_id', '=',Auth::user()->id)->get();
有什么理由解决这个问题吗?
【问题讨论】:
标签:
php
eloquent
laravel-5
pivot-table
【解决方案1】:
正确答案:
public function userProjects(){
$adminIdCondition = function($q){
$q->where('admin_id', Auth::user()->admin_id);
};
$projects = Project::with(['groups' => $adminIdCondition])
->whereHas('groups', $adminIdCondition)
->get();
return $projects;
}