DepartmentScope.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Scopes;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Scope;
  6. //判断所属部门和顶级部门
  7. class DepartmentScope implements Scope
  8. {
  9. public $user = [];
  10. public $search = [];
  11. public function __construct($user = [], $search = [])
  12. {
  13. $this->user = $user;
  14. $this->search = $search;
  15. }
  16. public function apply(Builder $builder, Model $model)
  17. {
  18. if(empty($this->search['top_depart_id'])){
  19. //默认进来 只显示自己公司下的 自己权限范围下的部门数据
  20. $top_depart_id = $this->user['depart_top'][0] ?? [];
  21. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  22. }else{
  23. //查询给的公司下的 自己权限范围下的部门数据
  24. $top_depart_id = $this->search['top_depart_id'];
  25. }
  26. $builder->where('top_depart_id', $top_depart_id)
  27. ->whereIn('depart_id', $this->user['depart_range']);
  28. }
  29. }