1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Repositories\Criteria\Course;
- 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 ($title = $this->request->get('title')) {
- $model = $model->where('title', 'like', "%{$title}%");
- }
- if (($status = $this->request->get('status', false)) !== false) {
- $model = $model->where('status', '=', $status);
- }
- if ($category_id = $this->request->get('category_id', false)) {
- $model = $model->where('category_id', '=', $category_id);
- }
- if ($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 {
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- }
- return $model;
- }
- }
|