Criteria.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Criteria;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Http\Request;
  13. use Prettus\Repository\Contracts\CriteriaInterface;
  14. use Prettus\Repository\Contracts\RepositoryInterface;
  15. abstract class Criteria implements CriteriaInterface
  16. {
  17. /**
  18. * @var Request
  19. */
  20. protected $request;
  21. public function __construct(Request $request)
  22. {
  23. $this->request = $request;
  24. }
  25. public function apply($model, RepositoryInterface $repository)
  26. {
  27. $model = $this->before($model);
  28. $model = $model->where(function ($query) {
  29. /** @var Builder $query */
  30. $this->condition($query);
  31. });
  32. return $this->after($model);
  33. }
  34. /**
  35. * Splice the query according to the parameters in the request.
  36. *
  37. * @param Builder $query
  38. */
  39. protected function condition(Builder $query): void
  40. {
  41. }
  42. protected function before($model)
  43. {
  44. return $model;
  45. }
  46. protected function after($model)
  47. {
  48. return $model;
  49. }
  50. }