DepartmentScope.php 937 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Scopes;
  3. use App\Model\Employee;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Scope;
  7. class DepartmentScope implements Scope
  8. {
  9. public $user = [];
  10. public function __construct($user = [])
  11. {
  12. $this->user = $user;
  13. }
  14. public function apply(Builder $builder, Model $model)
  15. {
  16. if($this->user['id'] != Employee::SPECIAL_ADMIN) {
  17. $depart_id = $this->user['depart_range'];
  18. $user_id = $this->user['id'];
  19. $builder->orwhere(function ($query) use ($depart_id,$user_id){
  20. $query->where(function ($query_sub) use ($depart_id,$user_id){
  21. $query_sub->whereIn('depart_id', $depart_id)
  22. ->orWhereIn('top_depart_id',$depart_id)
  23. ->orWhere('crt_id', $user_id);
  24. });
  25. });
  26. }
  27. }
  28. }