2015-12-06 107 views
0

我有一个我在Laravel 5中编写的时间表应用程序。现在我想根据用户权限(角色)选择时间表。我有3个级别的用户。Laravel 5根据用户角色选择行的范围

  • 用户
  • 管理员
  • 主管

主管取代一个用户管理取代主管

管理员用户应该能够看到所有用户时间表。主管应该只能看到与其关联的用户的时间表。用户只能看到他们自己的时间表。

我有以下方法设置来选择时间表等待批准。此方法仅适用于用户级别,因为我添加了whereUserId子句。

如何让以下方法适用于用户(管理员,用户,主管)的所有角色?

/** 
* Load timesheets awaiting approval. 
* 
*/ 
public function timesheetsAwaitingApproval() 
{ 
    return $this->timesheet->whereUserId($this->userid)->whereStatus('Submitted')->get(); 
} 
+0

这个timesheetsAwaitingApproval()方法在哪里定义? –

+0

您必须与主管和用户建立关系,以了解哪些用户与主管关联。 – smartrahat

回答

0

尝试使用模型之间的hasManyThrough关系,以获得您需要的时间表,检查doc。当然,如果你

监事例如

public function timesheets() 
{ 
    return $this->hasManyThrough('Timesheet::class', 'User::class', 'timesheet_id', 'user_id'); 
} 

public function timesheetsAwaitingApproval() 
{ 
    return $this->timesheets->whereStatus('Submitted')->get(); 
} 

或者类似的东西,那只是一个想法,可能会为你工作,这只会工作。