PrescriptionCriteria.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Repositories\Criteria\TCM;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. /**
  7. * Class PrescriptionCriteria.
  8. *
  9. * @package namespace App\Repositories\Criteria\TCM;
  10. */
  11. class PrescriptionCriteria implements CriteriaInterface
  12. {
  13. /**
  14. * Apply criteria in query repository
  15. *
  16. * @param string $model
  17. * @param RepositoryInterface $repository
  18. *
  19. * @return mixed
  20. */
  21. public function __construct(Request $request = null)
  22. {
  23. if (is_null($request)) {
  24. $this->request = \request();
  25. } else {
  26. $this->request = $request;
  27. }
  28. }
  29. /**
  30. * Apply criteria in query repository
  31. *
  32. * @param string $model
  33. * @param RepositoryInterface $repository
  34. *
  35. * @return mixed
  36. */
  37. public function apply($model, RepositoryInterface $repository)
  38. {
  39. if ($name = $this->request->get('name')) {
  40. $model = $model->whereHas('patient', function ($query) use ($name) {
  41. return $query->where('name', 'like', "%{$name}%");
  42. });
  43. }
  44. if ($patient_id = $this->request->get('patient_id')) {
  45. $model = $model->where('patient_id', $patient_id);
  46. }
  47. if ($medical_record_id = $this->request->get('medical_record_id')) {
  48. $model = $model->where('medical_record_id', $medical_record_id);
  49. }
  50. if (($status = $this->request->get('status', false)) !== false) {
  51. $model = $model->where('status', '=', $status);
  52. }
  53. $model = $model->orderByDesc('id');
  54. return $model;
  55. }
  56. }