PlanFilter.php 965 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\ModelFilters;
  3. use Carbon\Carbon;
  4. use EloquentFilter\ModelFilter;
  5. class PlanFilter 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 apply($query)
  15. {
  16. return $query;
  17. }
  18. public function assetId($assetId)
  19. {
  20. return $this->where('asset_id', $assetId);
  21. }
  22. public function expired($expired): PlanFilter
  23. {
  24. if (! in_array($expired, ['yes', 'no'])) {
  25. return $this;
  26. }
  27. return $this->when($expired == "yes", function ($query) {
  28. return $query->where('end', "<=", Carbon::now()->toDateString());
  29. })->when($expired == "no", function ($query) {
  30. return $query->where('end', ">", Carbon::now()->toDateString())->orWhereNull('end');
  31. });
  32. }
  33. }