MultiWhere.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * User: Mike
  4. * Email: m@9026.com
  5. * Date: 2017/1/12
  6. * Time: 17:52
  7. */
  8. namespace App\Repositories\User\Cash\Criteria;
  9. use App\Repositories\Base\Criteria;
  10. use App\Repositories\Contracts\RepositoryInterface as Repository;
  11. class MultiWhere extends Criteria {
  12. private $search = [];
  13. /**
  14. * MultiWhere constructor.
  15. * @param array $search
  16. *
  17. */
  18. public function __construct(array $search)
  19. {
  20. $this->search = $search;
  21. }
  22. /**
  23. * @param $model
  24. * @param RepositoryInterface $repository
  25. * @return mixed
  26. */
  27. public function apply($model, Repository $repository)
  28. {
  29. if(isset($this->search['keyword']) && $this->search['keyword']) {
  30. $keyword = '%'.$this->search['keyword'].'%';
  31. $model = $model->whereHas('user', function ($query) use ($keyword) {
  32. $query->where('nickname','like','$keyword');
  33. })->orWhereHas('bank', function ($query) use ($keyword) {
  34. $query->where('bank_name','like','$keyword')->
  35. orWhere('bank_number','like','$keyword');
  36. });
  37. }
  38. // dump($keyword);
  39. // dd($model->toSql());
  40. return $model;
  41. }
  42. }