MultiWhere.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if ($url !== false) {
  32. if(isset($this->search['keyword']) && ! empty($this->search['keyword'])) {
  33. $keywords = '%' . $this->search['keyword'] . '%';
  34. $model = $model->where(function ($query) use ($keywords) {
  35. $query->where('id' , 'like', $keywords)
  36. ->where('category','banner')
  37. ->orwhere('sort', 'like', $keywords);
  38. });
  39. }else{
  40. $model = $model->where('category','banner');
  41. }
  42. }else{ //sign标签
  43. if(isset($this->search['keyword']) && ! empty($this->search['keyword'])) {
  44. $keywords = '%' . $this->search['keyword'] . '%';
  45. $model = $model->where(function ($query) use ($keywords) {
  46. $query->where('id' , 'like', $keywords)
  47. ->where('category','sign')
  48. ->orwhere('value', 'like', $keywords);
  49. });
  50. }else{
  51. $model = $model->where('category','sign');
  52. }
  53. }
  54. return $model;
  55. }
  56. }