Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App;
- use Illuminate\Support\Facades\DB;
- class ModSorting
- {
- protected $request;
- protected $builder;
- public $filters;
- public $types;
- public $search;
- public function __construct($request, $builder)
- {
- $this->request = $request;
- $this->builder = $builder;
- }
- public function apply()
- {
- return $this->builder;
- }
- public function getFilters()
- {
- return $this->request->all();
- }
- public function getMods()
- {
- $this->builder = $this->builder->where('show_in_list','=', 1);
- $this->apply();
- foreach ($this->getFilters() as $filter => $value) {
- if (method_exists($this, $filter)) {
- $this->$filter($value);
- }
- }
- return $this->builder;
- }
- public function type($value)
- {
- $types = explode(',',$value);
- $this->types = $types;
- // Создаем функцию, чтобы в зависиммости от выбора типа выборки, подставлялись нужные значения в builder
- function getTypes ($q, $tt) {
- foreach ($tt as $type){
- switch ($type){
- case '1':
- $q->orWhereColumn('count','<','min_count');
- break;
- case '2':
- $q->orWhereColumn([
- ['count','<=','reg_count'],
- ['count','>=','min_count']
- ]);
- break;
- case '3':
- $q->orWhereColumn('count','>','reg_count');
- break;
- case '4' :
- $q->whereNotNull('info');
- }
- }
- }
- // Вклдючаем выборку по параметрам типов
- $this->builder->where(function ($q){
- getTypes($q, $this->types);
- });
- }
- public function category($value) {
- $this->builder = $this->builder->where('parent', '=', $value);
- return $this->builder;
- }
- public function search($value) {
- $this->search = $value;
- function getSearch($q, $value) {
- $q->where('pagetitle', 'LIKE', "%$value%")->orWhere('value', 'LIKE', "%$value%");
- }
- if ($value) {
- //Рабочий метод
- $this->builder = $this->builder->where(function ($q){
- getSearch($q, $this->search);
- });
- return $this->builder;
- }
- }
- public function status($value)
- {
- if ($value != 0) {
- $this->builder = $this->builder->where('status', '=', $value);
- }else {
- $this->builder->whereNotNull('status');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement