PlanFilter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\ModelFilters;
  3. use App\Models\Asset;
  4. use Carbon\Carbon;
  5. use EloquentFilter\ModelFilter;
  6. class PlanFilter extends ModelFilter
  7. {
  8. /**
  9. * Related Models that have ModelFilters as well as the method on the ModelFilter
  10. * As [relationMethod => [input_key1, input_key2]].
  11. *
  12. * @var array
  13. */
  14. public $relations = [];
  15. public function assetId($assetId): PlanFilter
  16. {
  17. $assetIds = Asset::query()->where('path','like','%'.$assetId.'%')->pluck('id');
  18. if ($assetIds->count() === 1 || $assetIds->isEmpty()) {
  19. // 如果没有子级,只返回该分组的需求
  20. return $this->where('asset_id', $assetId);
  21. }
  22. return $this->whereIn('asset_id',$assetIds);
  23. }
  24. public function expired($expired)
  25. {
  26. if (! in_array($expired, ['yes', 'no'])) {
  27. return $this;
  28. }
  29. return $this->when($expired == "yes", function ($query) {
  30. return $query->where('end', "<=", Carbon::now()->toDateString());
  31. })->when($expired == "no", function ($query) {
  32. return $query->where('end', ">", Carbon::now()->toDateString());
  33. });
  34. }
  35. }