├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── ScoutDataTable.php └── Traits └── ScoutDataTableTrait.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Laravel DataTables Scout Plugin 2 | 3 | ## Change Log 4 | 5 | ### v1.0-dev - UNRELEASED 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2013-2017 Arjay Angeles 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel DataTables Scout Plugin 2 | 3 | [![Laravel 5.4|5.5](https://img.shields.io/badge/Laravel-5.4|5.5-orange.svg)](http://laravel.com) 4 | [![Latest Stable Version](https://img.shields.io/packagist/v/yajra/laravel-datatables-scout.svg)](https://packagist.org/packages/yajra/laravel-datatables-scout) 5 | [![Build Status](https://travis-ci.org/yajra/laravel-datatables-scout.svg?branch=master)](https://travis-ci.org/yajra/laravel-datatables-scout) 6 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yajra/laravel-datatables-scout/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yajra/laravel-datatables-scout/?branch=master) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/yajra/laravel-datatables-scout.svg)](https://packagist.org/packages/yajra/laravel-datatables-scout) 8 | [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/yajra/laravel-datatables-scout) 9 | 10 | This package is a plugin of [Laravel DataTables](https://github.com/yajra/laravel-datatables) to support searching using [Laravel Scout](https://github.com/laravel/scout). 11 | 12 | ## Requirements 13 | - [PHP >=7.0](http://php.net/) 14 | - [Laravel 5.4|5.5](https://github.com/laravel/framework) 15 | - [Laravel Scout](https://github.com/laravel/scout) 16 | - [Laravel DataTables](https://github.com/yajra/laravel-datatables) 17 | - [jQuery DataTables v1.10.x](http://datatables.net/) 18 | 19 | ## Documentations 20 | - [Laravel DataTables Documentation](http://yajrabox.com/docs/laravel-datatables) 21 | 22 | ## Installation 23 | `composer require yajra/laravel-datatables-scout:^1.0` 24 | 25 | ## Usage 26 | 27 | ### Use the dataTable class directly. 28 | 29 | ```php 30 | use Yajra\DataTables\ScoutDataTable; 31 | 32 | $model = new App\User; 33 | 34 | return (new ScoutDataTable($model))->toJson() 35 | ``` 36 | 37 | ### Use via trait. 38 | 1. You need to use `ScoutDataTable` trait on your model. 39 | 40 | ```php 41 | use Yajra\DataTables\Traits\ScoutDataTable; 42 | 43 | class User extends Model 44 | { 45 | use ScoutDataTable; 46 | } 47 | ``` 48 | 49 | 2. Process dataTable directly from your model. 50 | 51 | ```php 52 | Route::get('users/data', function() { 53 | return User::dataTable()->toJson(); 54 | }); 55 | ``` 56 | 57 | ## Contributing 58 | 59 | Please see [CONTRIBUTING](https://github.com/yajra/laravel-datatables-scout/blob/master/.github/CONTRIBUTING.md) for details. 60 | 61 | ## Security 62 | 63 | If you discover any security related issues, please email [aqangeles@gmail.com](mailto:aqangeles@gmail.com) instead of using the issue tracker. 64 | 65 | ## Credits 66 | 67 | - [Arjay Angeles](https://github.com/yajra) 68 | - [All Contributors](https://github.com/yajra/laravel-datatables-scout/graphs/contributors) 69 | 70 | ## License 71 | 72 | The MIT License (MIT). Please see [License File](https://github.com/yajra/laravel-datatables-scout/blob/master/LICENSE.md) for more information. 73 | 74 | ## Buy me a beer 75 | Click here to lend your support to: Laravel DataTables and make a donation at pledgie.com ! 76 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yajra/laravel-datatables-scout", 3 | "description": "Laravel DataTables Scout Plugin.", 4 | "keywords": [ 5 | "laravel", 6 | "datatables", 7 | "scout", 8 | "plugin" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Arjay Angeles", 14 | "email": "aqangeles@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=7.0", 19 | "yajra/laravel-datatables-oracle": "8.*", 20 | "laravel/scout": "^3.0" 21 | }, 22 | "require-dev": { 23 | "orchestra/testbench": "~3.5" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Yajra\\DataTables\\": "src/" 28 | } 29 | }, 30 | "autoload-dev": { 31 | "psr-4": { 32 | "Yajra\\DataTables\\Tests\\": "tests/" 33 | } 34 | }, 35 | "extra": { 36 | "branch-alias": { 37 | "dev-master": "1.0-dev" 38 | } 39 | }, 40 | "minimum-stability": "dev", 41 | "prefer-stable": true 42 | } 43 | -------------------------------------------------------------------------------- /src/ScoutDataTable.php: -------------------------------------------------------------------------------- 1 | model = $model; 35 | $this->request = resolve('datatables.request'); 36 | $this->config = resolve('datatables.config'); 37 | $this->builder = new Builder($this->model, $this->request->keyword()); 38 | } 39 | 40 | /** 41 | * Get results. 42 | * 43 | * @return mixed 44 | */ 45 | public function results() 46 | { 47 | // Intentionally left blank. 48 | } 49 | 50 | /** 51 | * Count results. 52 | * 53 | * @return integer 54 | */ 55 | public function count() 56 | { 57 | // Intentionally left blank. 58 | } 59 | 60 | /** 61 | * Count total items. 62 | * 63 | * @return integer 64 | */ 65 | public function totalCount() 66 | { 67 | // Intentionally left blank. 68 | } 69 | 70 | /** 71 | * Perform column search. 72 | * 73 | * @return void 74 | */ 75 | public function columnSearch() 76 | { 77 | // Intentionally left blank. 78 | } 79 | 80 | /** 81 | * Perform pagination. 82 | * 83 | * @return void 84 | */ 85 | public function paging() 86 | { 87 | // Intentionally left blank. 88 | } 89 | 90 | /** 91 | * Organizes works. 92 | * 93 | * @param bool $mDataSupport 94 | * @return \Illuminate\Http\JsonResponse 95 | */ 96 | public function make($mDataSupport = true) 97 | { 98 | try { 99 | $limit = $this->request->get('length', 10); 100 | $start = $this->request->get('start', 0); 101 | $page = ($start / $limit) + 1; 102 | $results = $this->builder->paginate($limit, 'page', $page); 103 | $this->totalRecords = $results->total(); 104 | $this->filteredRecords = $this->totalRecords; 105 | 106 | $processed = $this->processResults($results->items(), $mDataSupport); 107 | $output = $this->transform($results, $processed); 108 | 109 | $this->collection = collect($output); 110 | $this->ordering(); 111 | 112 | return $this->render($this->collection->values()->all()); 113 | } catch (\Exception $exception) { 114 | return $this->errorResponse($exception); 115 | } 116 | } 117 | 118 | /** 119 | * @param string $keyword 120 | */ 121 | protected function globalSearch($keyword) 122 | { 123 | // Intentionally left blank. 124 | } 125 | 126 | /** 127 | * Append debug parameters on output. 128 | * 129 | * @param array $output 130 | * @return array 131 | */ 132 | protected function showDebugger(array $output) 133 | { 134 | $output['input'] = $this->request->all(); 135 | 136 | return $output; 137 | } 138 | 139 | /** 140 | * Resolve callback parameter instance. 141 | * 142 | * @return mixed 143 | */ 144 | protected function resolveCallbackParameter() 145 | { 146 | return $this->builder; 147 | } 148 | 149 | /** 150 | * Perform default query orderBy clause. 151 | */ 152 | protected function defaultOrdering() 153 | { 154 | $criteria = $this->request->orderableColumns(); 155 | if (! empty($criteria)) { 156 | $sorter = function ($a, $b) use ($criteria) { 157 | foreach ($criteria as $orderable) { 158 | $column = $this->getColumnName($orderable['column']); 159 | $direction = $orderable['direction']; 160 | if ($direction === 'desc') { 161 | $first = $b; 162 | $second = $a; 163 | } else { 164 | $first = $a; 165 | $second = $b; 166 | } 167 | if ($this->config->isCaseInsensitive()) { 168 | $cmp = strnatcasecmp($first[$column], $second[$column]); 169 | } else { 170 | $cmp = strnatcmp($first[$column], $second[$column]); 171 | } 172 | if ($cmp != 0) { 173 | return $cmp; 174 | } 175 | } 176 | // all elements were equal 177 | return 0; 178 | }; 179 | $this->collection = $this->collection->sort($sorter); 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /src/Traits/ScoutDataTableTrait.php: -------------------------------------------------------------------------------- 1 |