DemandFilters.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Filters;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Auth;
  5. class DemandFilters
  6. {
  7. protected $request;
  8. protected $queryBuilder;
  9. protected $filters = ['created_at_start', 'created_at_end', 'type'];
  10. protected $array_filter;
  11. protected $params = [];
  12. public function __construct(Request $request)
  13. {
  14. $this->request = $request;
  15. $this->params = $request->all();
  16. $this->array_filter = array_filter($this->request->only($this->filters));
  17. }
  18. public function apply($builder)
  19. {
  20. $this->queryBuilder = $builder;
  21. $this->before();
  22. foreach ($this->array_filter as $filter => $value) {
  23. if (method_exists($this, $filter)) {
  24. $this->$filter($value, $this->queryBuilder);
  25. }
  26. }
  27. return $this->queryBuilder;
  28. }
  29. public function created_at_start($created_at)
  30. {
  31. $this->queryBuilder->where('demands.created_at', '>=', $created_at . ' 00:00:00');
  32. }
  33. public function created_at_end($created_at)
  34. {
  35. $this->queryBuilder->where('demands.created_at', '<=', $created_at . ' 23:59:59');
  36. }
  37. public function type($type)
  38. {
  39. $this->queryBuilder->where('demands.type',$type);
  40. }
  41. public function before()
  42. {
  43. $user = Auth::user();
  44. if(!isset($user))return;
  45. if($user->isSuperAdmin()){
  46. return;
  47. }
  48. $authorities = $user->authorities();
  49. $authorities = data_get($authorities,'*.id');
  50. $authorities[] = null;
  51. $this->queryBuilder->whereIn('authority_id',$authorities);
  52. }
  53. }