MultiWhere.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Album\Xyx\Criteria;
  9. use App\Repositories\Base\Criteria;
  10. use App\Repositories\Contracts\RepositoryInterface as Repository;
  11. class MultiWhere extends Criteria {
  12. private $search = [];
  13. public function __construct(array $search,int $store_id)
  14. {
  15. $this->search = $search;
  16. $this->store_id = $store_id;
  17. }
  18. /**
  19. * @param $model
  20. * @param RepositoryInterface $repository
  21. * @return mixed
  22. */
  23. public function apply($model, Repository $repository)
  24. {
  25. if(isset($this->search['keyword']) && $this->search['keyword']) {
  26. $keyword = '%'.$this->search['keyword'].'%';
  27. $model = $model->where([['username','like',$keyword],['store_id',$this->store_id]])
  28. ->orwhere([['id','like',$keyword],['store_id',$this->store_id]]);
  29. } else if(isset($this->search['updated_at']) && $this->search['updated_at']) {
  30. $model = $model->where('updated_at',$this->search['updated_at']);
  31. } else {
  32. $model = $model->where([['store_id',$this->store_id]]);
  33. }
  34. return $model;
  35. }
  36. }