DepartmentScope.php 1001 B

1234567891011121314151617181920212223242526272829303132
  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. class DepartmentScope implements Scope
  7. {
  8. public $user = [];
  9. public $search = [];
  10. public function __construct($user = [], $search = [])
  11. {
  12. $this->user = $user;
  13. $this->search = $search;
  14. }
  15. public function apply(Builder $builder, Model $model)
  16. {
  17. if(empty($this->search['top_depart_id'])){
  18. //默认进来 只显示自己公司下的 自己权限范围下的部门数据
  19. $top_depart_id = $this->user['depart_top'][0] ?? [];
  20. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  21. }else{
  22. //查询给的公司下的 自己权限范围下的部门数据
  23. $top_depart_id = $this->search['top_depart_id'];
  24. }
  25. $builder->where('top_depart_id', $top_depart_id)
  26. ->whereIn('depart_id', $this->user['depart_range']);
  27. }
  28. }