MultiWhere.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Settings\Banner\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. //有返回/banner/首次出现的位置 无返回false
  30. $url = strpos(url()->current(), '/banner/');
  31. $appid = $this->search['appid'];
  32. if ($url !== false) {
  33. if(isset($this->search['keyword']) && ! empty($this->search['keyword'])) {
  34. $keywords = '%' . $this->search['keyword'] . '%';
  35. $model = $model->where(function ($query) use ($keywords) {
  36. $query->where('id' , 'like', $keywords)
  37. ->where('category','banner')
  38. ->orwhere('sort', 'like', $keywords);
  39. });
  40. }else{
  41. $model = $model->where('category','banner');
  42. }
  43. }else{ //sign标签
  44. if(isset($this->search['keyword']) && ! empty($this->search['keyword'])) {
  45. $keywords = '%' . $this->search['keyword'] . '%';
  46. $model = $model->where(function ($query) use ($keywords) {
  47. $query->where('id' , 'like', $keywords)
  48. ->where('category','sign')
  49. ->orwhere('value', 'like', $keywords);
  50. });
  51. }else{
  52. $model = $model->where('category','sign');
  53. }
  54. }
  55. return $model->where('appid',$appid);
  56. }
  57. }