DictCriteria.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Repositories\Criteria\Base;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. class DictCriteria implements CriteriaInterface
  7. {
  8. /**
  9. * @var \Illuminate\Http\Request
  10. */
  11. protected $request;
  12. public function __construct(Request $request)
  13. {
  14. $this->request = $request;
  15. }
  16. /**
  17. * @param $model
  18. * @param RepositoryInterface $repository
  19. *
  20. * @return mixed
  21. */
  22. public function apply($model, RepositoryInterface $repository)
  23. {
  24. // this is a Example
  25. //if ($this->request->filled('name')) {
  26. // $model = $model->where('name', '=', $this->request->get('name'));
  27. //}
  28. if ($this->request->filled('name')) {
  29. $model = $model->where('name', 'like', '%' . $this->request->get('name') . '%');
  30. }
  31. if ($this->request->filled('code')) {
  32. $model = $model->where('code', 'like', '%' . $this->request->get('code') . '%');
  33. }
  34. if ($this->request->filled('codes')) {
  35. $model = $model->whereIn('code', str2arr($this->request->get('codes')));
  36. }
  37. return $model;
  38. }
  39. }