DepartmentScope.php 820 B

123456789101112131415161718192021222324252627282930
  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->where(function ($query) use ($depart_id,$user_id){
  20. $query->whereIn('depart_id', $depart_id)
  21. ->orWhereIn('top_depart_id',$depart_id)
  22. ->orWhere('crt_id', $user_id);
  23. });
  24. }
  25. }
  26. }