Overview

Namespaces

  • None
  • Yajra
    • Datatables
      • Contracts
      • Engines
      • Facades
      • Generators
      • Html
      • Processors
      • Services
      • Transformers

Classes

  • Yajra\Datatables\Datatables
  • Yajra\Datatables\DatatablesServiceProvider
  • Yajra\Datatables\Engines\BaseEngine
  • Yajra\Datatables\Engines\CollectionEngine
  • Yajra\Datatables\Engines\EloquentEngine
  • Yajra\Datatables\Engines\QueryBuilderEngine
  • Yajra\Datatables\Facades\Datatables
  • Yajra\Datatables\Generators\DataTablesMakeCommand
  • Yajra\Datatables\Generators\DataTablesScopeCommand
  • Yajra\Datatables\Helper
  • Yajra\Datatables\Html\Builder
  • Yajra\Datatables\Html\Column
  • Yajra\Datatables\Html\Parameters
  • Yajra\Datatables\Processors\DataProcessor
  • Yajra\Datatables\Processors\RowProcessor
  • Yajra\Datatables\Request
  • Yajra\Datatables\Services\DataTable
  • Yajra\Datatables\Transformers\DataTransformer

Interfaces

  • Yajra\Datatables\Contracts\DataTableButtonsContract
  • Yajra\Datatables\Contracts\DataTableContract
  • Yajra\Datatables\Contracts\DataTableEngineContract
  • Yajra\Datatables\Contracts\DataTableScopeContract

Functions

  • config_path
  • public_path
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Yajra\Datatables\Engines;
  4: 
  5: use Closure;
  6: use Illuminate\Contracts\Support\Arrayable;
  7: use Illuminate\Support\Arr;
  8: use Illuminate\Support\Collection;
  9: use Illuminate\Support\Str;
 10: use Yajra\Datatables\Request;
 11: 
 12: /**
 13:  * Class CollectionEngine.
 14:  *
 15:  * @package Yajra\Datatables\Engines
 16:  * @author  Arjay Angeles <aqangeles@gmail.com>
 17:  */
 18: class CollectionEngine extends BaseEngine
 19: {
 20:     /**
 21:      * Collection object
 22:      *
 23:      * @var \Illuminate\Support\Collection
 24:      */
 25:     public $collection;
 26: 
 27:     /**
 28:      * Collection object
 29:      *
 30:      * @var \Illuminate\Support\Collection
 31:      */
 32:     public $original_collection;
 33: 
 34:     /**
 35:      * CollectionEngine constructor.
 36:      *
 37:      * @param \Illuminate\Support\Collection $collection
 38:      * @param \Yajra\Datatables\Request $request
 39:      */
 40:     public function __construct(Collection $collection, Request $request)
 41:     {
 42:         $this->request             = $request;
 43:         $this->collection          = $collection;
 44:         $this->original_collection = $collection;
 45:         $this->columns             = array_keys($this->serialize($collection->first()));
 46:     }
 47: 
 48:     /**
 49:      * Serialize collection
 50:      *
 51:      * @param  mixed $collection
 52:      * @return mixed|null
 53:      */
 54:     protected function serialize($collection)
 55:     {
 56:         return $collection instanceof Arrayable ? $collection->toArray() : (array) $collection;
 57:     }
 58: 
 59:     /**
 60:      * Set auto filter off and run your own filter.
 61:      * Overrides global search.
 62:      *
 63:      * @param \Closure $callback
 64:      * @param bool $globalSearch
 65:      * @return $this
 66:      */
 67:     public function filter(Closure $callback, $globalSearch = false)
 68:     {
 69:         $this->overrideGlobalSearch($callback, $this, $globalSearch);
 70: 
 71:         return $this;
 72:     }
 73: 
 74:     /**
 75:      * Append debug parameters on output.
 76:      *
 77:      * @param  array $output
 78:      * @return array
 79:      */
 80:     public function showDebugger(array $output)
 81:     {
 82:         $output["input"] = $this->request->all();
 83: 
 84:         return $output;
 85:     }
 86: 
 87:     /**
 88:      * Count total items.
 89:      *
 90:      * @return integer
 91:      */
 92:     public function totalCount()
 93:     {
 94:         return $this->totalRecords ? $this->totalRecords : $this->collection->count();
 95:     }
 96: 
 97:     /**
 98:      * Count results.
 99:      *
100:      * @return integer
101:      */
102:     public function count()
103:     {
104:         return $this->collection->count() > $this->totalRecords ? $this->totalRecords : $this->collection->count();
105:     }
106: 
107:     /**
108:      * Perform sorting of columns.
109:      *
110:      * @return void
111:      */
112:     public function ordering()
113:     {
114:         if ($this->orderCallback) {
115:             call_user_func($this->orderCallback, $this);
116: 
117:             return;
118:         }
119: 
120:         foreach ($this->request->orderableColumns() as $orderable) {
121:             $column           = $this->getColumnName($orderable['column']);
122:             $this->collection = $this->collection->sortBy(
123:                 function ($row) use ($column) {
124:                     $data = $this->serialize($row);
125: 
126:                     return Arr::get($data, $column);
127:                 }
128:             );
129: 
130:             if ($orderable['direction'] == 'desc') {
131:                 $this->collection = $this->collection->reverse();
132:             }
133:         }
134:     }
135: 
136:     /**
137:      * Perform global search.
138:      *
139:      * @return void
140:      */
141:     public function filtering()
142:     {
143:         $columns          = $this->request['columns'];
144:         $this->collection = $this->collection->filter(
145:             function ($row) use ($columns) {
146:                 $data                  = $this->serialize($row);
147:                 $this->isFilterApplied = true;
148:                 $found                 = [];
149: 
150:                 $keyword = $this->request->keyword();
151:                 foreach ($this->request->searchableColumnIndex() as $index) {
152:                     $column = $this->getColumnName($index);
153:                     if (! $value = Arr::get($data, $column)) {
154:                         continue;
155:                     }
156: 
157:                     if ($this->isCaseInsensitive()) {
158:                         $found[] = Str::contains(Str::lower($value), Str::lower($keyword));
159:                     } else {
160:                         $found[] = Str::contains($value, $keyword);
161:                     }
162:                 }
163: 
164:                 return in_array(true, $found);
165:             }
166:         );
167:     }
168: 
169:     /**
170:      * Perform column search.
171:      *
172:      * @return void
173:      */
174:     public function columnSearch()
175:     {
176:         $columns = $this->request->get('columns');
177:         for ($i = 0, $c = count($columns); $i < $c; $i++) {
178:             if ($this->request->isColumnSearchable($i)) {
179:                 $this->isFilterApplied = true;
180:                 $regex = $this->request->isRegex($i);
181: 
182:                 $column  = $this->getColumnName($i);
183:                 $keyword = $this->request->columnKeyword($i);
184: 
185:                 $this->collection = $this->collection->filter(
186:                     function ($row) use ($column, $keyword, $regex) {
187:                         $data = $this->serialize($row);
188: 
189:                         $value = Arr::get($data, $column);
190: 
191:                         if ($this->isCaseInsensitive()) {
192:                             if ($regex) {
193:                                 return preg_match('/' . $keyword . '/i', $value) == 1;
194:                             } else {
195:                                 return strpos(Str::lower($value), Str::lower($keyword)) !== false;
196:                             }
197:                         } else {
198:                             if ($regex) {
199:                                 return preg_match('/' . $keyword . '/', $value) == 1;
200:                             } else {
201:                                 return strpos($value, $keyword) !== false;
202:                             }
203:                         }
204:                     }
205:                 );
206:             }
207:         }
208:     }
209: 
210:     /**
211:      * Perform pagination.
212:      *
213:      * @return void
214:      */
215:     public function paging()
216:     {
217:         $this->collection = $this->collection->slice(
218:             $this->request['start'],
219:             (int)$this->request['length'] > 0 ? $this->request['length'] : 10
220:         );
221:     }
222: 
223:     /**
224:      * Get results.
225:      *
226:      * @return mixed
227:      */
228:     public function results()
229:     {
230:         return $this->collection->all();
231:     }
232: 
233:     /**
234:      * Organizes works.
235:      *
236:      * @param bool $mDataSupport
237:      * @param bool $orderFirst
238:      * @return \Illuminate\Http\JsonResponse
239:      */
240:     public function make($mDataSupport = false, $orderFirst = true)
241:     {
242:         return parent::make($mDataSupport, $orderFirst);
243:     }
244: }
245: 
API documentation generated by ApiGen