RoomCriteria.php 1.4 KB

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