TopDepartmentScope.php 960 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. //只判断顶级id
  7. class TopDepartmentScope 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. }
  28. }