12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Repositories\Criteria\Course;
- use App\Repositories\Enums\ModelStatusEnum;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class CourseCriteria.
- *
- * @package namespace App\Repositories\Criteria;
- */
- class CourseCriteria 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('title')) {
- $title = $this->request->get('title');
- $model = $model->where('title', 'like', "%{$title}%");
- }
- if ($this->request->filled('status')) {
- $status = $this->request->get('status', 0);
- $model = $model->where('status', '=', $status);
- }
- if ($this->request->filled('category_id')) {
- $category_id = $this->request->get('category_id', 0);
- $model = $model->where('category_id', '=', $category_id);
- }
- if ($this->request->filled('tag')) {
- $tag = $this->request->get('tag');
- switch ($tag) {
- case 'new':
- $model = $model->orderByDesc('id');
- break;
- case 'hot':
- $model = $model->orderByDesc('user_count')->orderByDesc('id');
- break;
- case 'rec':
- $model = $model->orderByDesc('is_rec')->orderByDesc('id');
- break;
- }
- } else {
- if (!$this->request->filled('orderBy')) {
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- }
- }
- if (isApi()) {
- $model = $model->where('status', ModelStatusEnum::OK)->where('published_at', '<=', Carbon::now()->toDateTimeString());
- }
- return $model;
- }
- }
|