1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Repositories\Criteria\Note;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class NoteCriteria.
- *
- * @package namespace App\Repositories\Criteria\Note;
- */
- class NoteCriteria implements CriteriaInterface
- {
- 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('name', 'like', "%{$title}%");
- }
- if ($course_category_id = $this->request->get('course_category_id')) {
- $model = $model->where('course_category_id', '=', $course_category_id);
- }
- if ($tags = $this->request->get('tags')) {
- $model = $model->where('tags', 'like', "%-{$tags}-%");
- }
- if (($status = $this->request->get('status', false)) !== false) {
- $model = $model->where('status', '=', $status);
- }
- if ($id = $this->request->get('id')) {
- $model = $model->where('id', '=', $id);
- }
- if (($is_release = $this->request->get('is_release', false)) !== false) {
- $model = $model->where('is_release', '=', $is_release);
- }
- if (($type = $this->request->get('type', false)) !== false) {
- $model = $model->where('type', '=', $type);
- }
- if ($this->request->get('me')) {
- $model = $model->where('user_id', login_user_id());
- }
- if ($tag = $this->request->get('tag')) {
- switch ($tag) {
- case 'new':
- $model = $model->orderByDesc('id');
- break;
- case 'hot':
- $model = $model->orderByDesc('good_count')->orderByDesc('id');
- break;
- }
- } else {
- $model = $model->orderByDesc('id');
- }
- return $model;
- }
- }
|