简体   繁体   English

如何在Laravel 5.2中正确设置透视表的雄辩关系

[英]How to correctly setup the eloquent relationships for pivot tables in Laravel 5.2

I have three tables in a db which are as follows (not all of the columns but minimised for clarity): 我在数据库中有三个表,如下所示(不是所有列,而是为了清楚起见将其最小化):

admins (id, community_id, email, password, level)
community (community_id, community_name)
community_results (community_result_id, community_session_id)
community_sessions (community_session_id, community_id)

I have setup the associated models and within my admin controller I have the following code that simply grabs all the results in the database into a laravel collection (and this works fine). 我已经设置了相关的模型,并且在我的管理控制器中,我有以下代码,可以将数据库中的所有结果简单地捕获到laravel集合中(这很好用)。 The admin table contains two types of admin user - one is the 'superuser' that has access to every single row in the db, and the other type is a 'community admin' and will only have access to their community results (defined by the level column in the admins table). 管理员表包含两种类型的管理员用户-一种是“超级用户”,可以访问数据库中的每一行,另一种类型是“社区管理员”,并且只能访问其社区结果(由管理员表中的“级别”列)。

When logged into the admin as a 'community admin' I want to only get results for their community (admins.community_id is the foreign key in this instance that relates this admin to a community). 当以“社区管理员”身份登录到管理员时,我只希望获取其社区的结果(在这种情况下,admins.community_id是将此管理员与社区相关联的外键)。

eg John Doe is a 'community admin' for the 'ACME Community' and belongs to the community_id 5, when logged in he will only get 'community results' for all of the 'community sessions' that relate to that particular community (community_id is the foreign key in the community_sessions table). 例如,John Doe是“ ACME社区”的“社区管理员”,属于community_id 5,登录后,他将仅获得与该特定社区相关的所有“社区会话”的“社区结果”(community_id为community_sessions表中的外键)。

So i'd need to find a way to create a modified version of the results() relation within the CommunitySession.php model but rather than query EVERY row only retrieve those of one or more community_session_id's - alternatively is there way using the Community.php model to create a relation to basically pull in results using the relation like the following... 因此,我需要找到一种方法来在CommunitySession.php模型内创建results()关系的修改版本,而不是查询每行仅检索一个或多个community_session_id的内容,或者也可以使用Community.php模型来创建一个关系,基本上使用如下关系来获取结果...

$community = Community::find(5);
$community->results; // pull results for this community using a relation

Can anyone suggest the best way to do this? 谁能建议最好的方法吗? Thanks in advance 提前致谢

class AdminResultController extends Controller
{
   public function index()
   {
      // if logged in as the 'superuser' admin get ALL results
      if (Auth::guard('admin')->user()->level == 1) 
      {
         $results = CommunityResult::all();
      } else {
        // logged in as 'standard' admin get only their community results
        $results = new CommunityResult;
        // how do I get collection of results for this community only
      }
   }
}

// CommunityResult.php (model) // CommunityResult.php(模型)

class CommunityResult extends Model
{
  public $primaryKey = 'community_result_id';

  public function session()
  {
    return $this->hasOne('App\CommunitySession', 'community_session_id', 'community_session_id');
  }
}

// CommunitySession.php (model) // CommunitySession.php(模型)

class CommunitySession extends Model
{
  public $primaryKey = 'community_session_id';

  public function community()
  {
    return $this->belongsTo('App\Community', 'community_id');
  }

  public function results()
  {
    return $this->hasMany('App\CommunityResult', 'community_session_id');
  }  
}

// Community.php (model) // Community.php(模型)

class Community extends Model
{
  public $table = 'community';
  public $primaryKey = 'community_id';

  public function sessions()
  {
    return $this->hasMany('App\CommunitySession', 'community_id');
  }
}
Auth::guard('admin')->user()->with('sessions.results')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM