1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Repositories\Criteria\User;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class StudentCriteria.
- *
- * @package namespace App\Repositories\Criteria\User\Student;
- */
- class UserCriteria implements CriteriaInterface
- {
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function __construct(Request $request = null)
- {
- if (is_null($request)) {
- $this->request = \request();
- } else {
- $this->request = $request;
- }
- }
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function apply($model, RepositoryInterface $repository)
- {
- if ($this->request->filled('turename')) {
- $turename = $this->request->get('turename');
- $model = $model->where('turename', 'like', "%{$turename}%");
- }
- if ($this->request->filled('username')) {
- $username = $this->request->get('username');
- $model = $model->where('username', 'like', "%{$username}%");
- }
- if ($this->request->filled('mobile')) {
- $mobile = $this->request->get('mobile');
- $model = $model->where('mobile', 'like', "%{$mobile}%");
- }
- if ($this->request->filled('class')) {
- $class = $this->request->get('class');
- $model = $model->where('class', 'like', "%{$class}%");
- }
- if ($this->request->filled('name')) {
- $name = $this->request->get('name');
- $model = $model->where('name', 'like', "%{$name}%");
- }
- if ($this->request->filled('sex')) {
- $sex = $this->request->get('sex');
- $model = $model->where('sex', '=', $sex);
- }
- if ($this->request->filled('role_id')) {
- $role_id = $this->request->get('role_id');
- $model = $model->where('role_id', '=', $role_id);
- }
- // if ($period = $this->request->get('period')) {
- // $model = $model->where('period', 'like', $period);
- // }
- if ($this->request->filled('organization_id')) {
- $organization_id = $this->request->get('organization_id');
- $model = $model->whereHas('organizations', function ($query) use ($organization_id) {
- return $query->where('organization_id', $organization_id);
- });
- }
- $model = $model->orderByDesc('id');
- return $model;
- }
- }
|