1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Datatables. |
9
|
|
|
* |
10
|
|
|
* @package Yajra\Datatables |
11
|
|
|
* @author Arjay Angeles <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Datatables |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Datatables request object. |
17
|
|
|
* |
18
|
|
|
* @var \Yajra\Datatables\Request |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* HTML builder instance. |
24
|
|
|
* |
25
|
|
|
* @var \Yajra\Datatables\Html\Builder |
26
|
|
|
*/ |
27
|
|
|
protected $html; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gets query and returns instance of class. |
31
|
|
|
* |
32
|
|
|
* @param mixed $source |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws \Exception |
35
|
|
|
*/ |
36
|
|
|
public static function of($source) |
37
|
|
|
{ |
38
|
|
|
$engines = config('datatables.engines'); |
39
|
|
|
$builders = config('datatables.builders'); |
40
|
|
|
|
41
|
|
|
if (is_array($source)) { |
42
|
|
|
$source = new Collection($source); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($builders as $class => $engine) { |
46
|
|
|
if ($source instanceof $class) { |
47
|
|
|
$class = $engines[$engine]; |
48
|
|
|
|
49
|
|
|
return new $class($source); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new \Exception('No available engine for ' . get_class($source)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get request object. |
58
|
|
|
* |
59
|
|
|
* @return \Yajra\Datatables\Request |
60
|
|
|
*/ |
61
|
|
|
public function getRequest() |
62
|
|
|
{ |
63
|
|
|
return $this->request; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Datatables using Query Builder. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Database\Query\Builder|mixed $builder |
70
|
|
|
* @return \Yajra\Datatables\Engines\QueryBuilderEngine |
71
|
|
|
*/ |
72
|
|
|
public function queryBuilder($builder) |
73
|
|
|
{ |
74
|
|
|
return new Engines\QueryBuilderEngine($builder); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Datatables using Eloquent Builder. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|mixed $builder |
81
|
|
|
* @return \Yajra\Datatables\Engines\EloquentEngine |
82
|
|
|
*/ |
83
|
|
|
public function eloquent($builder) |
84
|
|
|
{ |
85
|
|
|
return new Engines\EloquentEngine($builder); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Datatables using Collection. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Support\Collection|mixed $collection |
92
|
|
|
* @return \Yajra\Datatables\Engines\CollectionEngine |
93
|
|
|
*/ |
94
|
|
|
public function collection($collection) |
95
|
|
|
{ |
96
|
|
|
if (is_array($collection)) { |
97
|
|
|
$collection = new Collection($collection); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new Engines\CollectionEngine($collection); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get html builder instance. |
105
|
|
|
* |
106
|
|
|
* @return \Yajra\Datatables\Html\Builder |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function getHtmlBuilder() |
110
|
|
|
{ |
111
|
|
|
if (!class_exists('\Yajra\Datatables\Html\Builder')) { |
112
|
|
|
throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->html ?: $this->html = app('datatables.html'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|