1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Repositories\Criteria\Inform;
- use App\Repositories\Models\Inform\Category;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class InformationCriteria.
- *
- * @package namespace App\Repositories\Criteria\Inform;
- */
- class InformationCriteria 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');
- if ($title) $model = $model->where('title', 'like', "%{$title}%");
- }
- if ($this->request->filled('status')) {
- $status = $this->request->get('status');
- $model = $model->where('status', '=', $status);
- }
- if ($this->request->filled('category_id')) {
- $category_id = $this->request->get('category_id');
- if ($category_id) $model = $model->where('category_id', $category_id);
- }
- if ($organization_id = $this->request->get('organization_id')) {
- if ($organization_id) {
- $model = $model->where('organization_id', $organization_id);
- } else {
- $model = $model->where('organization_id', 0);
- }
- } else {
- $model = $model->where('organization_id', 0);
- }
- if ($tag = $this->request->get('tag')) {
- switch ($tag) {
- case 'new':
- $model = $model->orderByDesc('id');
- break;
- case 'hot':
- $model = $model->orderByDesc('view_count')->orderByDesc('id');
- break;
- case 'good':
- $model = $model->orderByDesc('good_count')->orderByDesc('id');
- break;
- }
- } else {
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- }
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- return $model;
- }
- }
|