ActionFilter.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\ModelFilters;
  3. use Carbon\Carbon;
  4. use EloquentFilter\ModelFilter;
  5. class ActionFilter extends ModelFilter
  6. {
  7. /**
  8. * Related Models that have ModelFilters as well as the method on the ModelFilter
  9. * As [relationMethod => [input_key1, input_key2]].
  10. *
  11. * @var array
  12. */
  13. public $relations = [];
  14. public function user($id)
  15. {
  16. return $this->where("created_by", $id);
  17. }
  18. public function time($time)
  19. {
  20. return match ($time) {
  21. "today" => $this->where("created_at", ">=", Carbon::now()->startOfDay()),
  22. "yesterday" => $this->where("created_at", ">=", Carbon::now()->subDay()->startOfDay())
  23. ->where("created_at", "<", Carbon::now()->startOfDay()),
  24. "this_week" => $this->where("created_at", ">=", Carbon::now()->startOfWeek()),
  25. "last_week" => $this->where("created_at", ">=", Carbon::now()->subWeek()->startOfWeek())
  26. ->where("created_at", "<", Carbon::now()->startOfWeek()),
  27. "this_month" => $this->where("created_at", ">=", Carbon::now()->startOfMonth()),
  28. "last_month" => $this->where("created_at", ">=", Carbon::now()->subMonth()->startOfMonth())
  29. ->where("created_at", "<", Carbon::now()->startOfMonth()),
  30. default => $this,
  31. };
  32. }
  33. }