AgentWhere.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Criteria;
  9. use App\Repositories\Base\Criteria;
  10. use App\Repositories\Contracts\RepositoryInterface as Repository;
  11. class AgentWhere extends Criteria {
  12. private $search = [];
  13. private $store_id;
  14. /**
  15. * MultiWhere constructor.
  16. * @param array $search
  17. *
  18. */
  19. public function __construct(array $search, int $store_id)
  20. {
  21. $this->search = $search;
  22. $this->store_id = $store_id;
  23. }
  24. /**
  25. * @param $model
  26. * @param RepositoryInterface $repository
  27. * @return mixed
  28. */
  29. public function apply($model, Repository $repository)
  30. {
  31. if (isset($this->search['keyword']) && $this->search['keyword']) {
  32. $keyword = '%' . $this->search['keyword'] . '%';
  33. $model = $model->where([['name','like',$keyword],['store_id',$this->store_id]])
  34. ->orwhere([['id','like',$keyword],['store_id',$this->store_id]])
  35. ->orwhere([['address','like',$keyword],['store_id',$this->store_id]])
  36. ->orwhere([['phone','like',$keyword],['store_id',$this->store_id]]);
  37. } else {
  38. $model = $model->where([['store_id', $this->store_id]]);
  39. }
  40. if (isset($this->search['status']) && $this->search['status']) {
  41. $status = $this->search['status'];
  42. $model = $model->whereHas('user', function ($model) use ($status) {
  43. if ($status == 3) {
  44. $model->where('is_boss', 1);
  45. }
  46. })->with(['user:username as nickname,is_boss']);
  47. if ($status == 2) {
  48. $model = $model->where([['status', 1]]);
  49. }
  50. if ($status == 1) {
  51. $model = $model->where([['status', 0]]);
  52. }
  53. }
  54. return $model;
  55. }
  56. }