UserMessageCriteria.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Repositories\Criteria\Base;
  3. use App\Repositories\Models\Base\UserMessage;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. class UserMessageCriteria implements CriteriaInterface
  9. {
  10. /**
  11. * @var \Illuminate\Http\Request
  12. */
  13. protected $request;
  14. public function __construct(Request $request)
  15. {
  16. $this->request = $request;
  17. }
  18. /**
  19. * @param $model
  20. * @param RepositoryInterface $repository
  21. *
  22. * @return mixed
  23. */
  24. public function apply($model, RepositoryInterface $repository)
  25. {
  26. if ($this->request->filled('name')) {
  27. $model = $model->where('name', 'like', '%' . $this->request->get('name') . '%');
  28. }
  29. if ($this->request->filled('is_read')) {
  30. $model = $model->where('is_read', '=', $this->request->get('is_read'));
  31. } else {
  32. $model = $model->where('is_read', '=', UserMessage::IS_READ_NO);
  33. }
  34. if ($this->request->filled('type')) {
  35. $model = $model->where('type', '=', $this->request->get('type'));
  36. }
  37. if ($this->request->filled('guard') || $this->request->header('guard')) {
  38. $guard = $this->request->get('guard', $this->request->header('guard'));
  39. $user = Auth::guard($guard)->user();
  40. $model = $model->where('user_id', $user->id)->where('user_type', get_class($user));
  41. } else {
  42. $user = login_admin();
  43. $model = $model->where('user_id', $user->id)->where('user_type', get_class($user));
  44. }
  45. $model = $model->orderByDesc('id');
  46. return $model;
  47. }
  48. }