123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace App\Repositories\Criteria\Base;
- use App\Repositories\Enums\Base\RoleEnum;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class AdminCriteria.
- *
- * @package namespace App\Repositories\Criteria\Base;
- */
- class AdminCriteria 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 ($name = $this->request->get('name', false)) {
- $model = $model->where('name', 'like', "%{$name}%");
- }
- if ($username = $this->request->get('username', false)) {
- $model = $model->where('username', 'like', "%{$username}%");
- }
- if (($status = $this->request->get('status', false)) !== false) {
- $model = $model->where('status', '=', $status);
- }
- if (is_admin_login()) {
- $admin = login_admin();
- if ($admin->role_id > RoleEnum::ADMIN) {
- $model = $model->where('shop_id', $admin->shop_id);
- }
- }
- $model = $model->where('is_admin', 1)->orderByDesc('id');
- return $model;
- }
- }
|