DepartmentScope.php 924 B

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. class DepartmentScope implements Scope
  7. {
  8. public $depart_id = [];
  9. public $is_main = 0;
  10. public $user_id = 0;
  11. public function __construct($depart_id = [], $is_main = 0, $user_id = 0)
  12. {
  13. if(! is_array($depart_id)) $depart_id = [$depart_id];
  14. $this->depart_id = $depart_id;
  15. $this->is_main = $is_main;
  16. $this->user_id = $user_id;
  17. }
  18. public function apply(Builder $builder, Model $model)
  19. {
  20. if(! $this->is_main) {
  21. $depart_id = $this->depart_id;
  22. $user_id = $this->user_id;
  23. $builder->where(function ($query) use ($depart_id,$user_id){
  24. $query->whereIn('depart_id', $depart_id)
  25. ->orWhere('crt_id', $user_id);
  26. });
  27. }
  28. }
  29. }