MultiWhere.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Repositories\User\Criteria;
  3. use App\Repositories\Base\Criteria;
  4. use App\Repositories\Contracts\RepositoryInterface as Repository;
  5. class MultiWhere extends Criteria {
  6. private $search = [];
  7. /**
  8. * MultiWhere constructor.
  9. * @param array $search
  10. *
  11. */
  12. public function __construct(array $search)
  13. {
  14. $this->search = $search;
  15. }
  16. /**
  17. * @param $model
  18. * @param RepositoryInterface $repository
  19. * @return mixed
  20. */
  21. public function apply($model, Repository $repository)
  22. {
  23. if(isset($this->search['keyword']) && ! empty($this->search['keyword'])) {
  24. $keywords = '%' . $this->search['keyword'] . '%';
  25. $model = $model->where(function ($query) use ($keywords) {
  26. $query->where('id' , 'like', $keywords)
  27. ->orwhere('username', 'like', $keywords)
  28. ->orwhere('email', 'like', $keywords)
  29. ->orwhere('mobile', 'like', $keywords);
  30. });
  31. }
  32. return $model;
  33. }
  34. }