1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\Macroable; |
6
|
|
|
|
7
|
|
|
class DataTables |
8
|
|
|
{ |
9
|
|
|
use Macroable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* DataTables request object. |
13
|
|
|
* |
14
|
|
|
* @var \Yajra\DataTables\Utilities\Request |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* HTML builder instance. |
20
|
|
|
* |
21
|
|
|
* @var \Yajra\DataTables\Html\Builder |
22
|
|
|
*/ |
23
|
|
|
protected $html; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Make a DataTable instance from source. |
27
|
|
|
* Alias of make for backward compatibility. |
28
|
|
|
* |
29
|
|
|
* @param mixed $source |
30
|
|
|
* @return mixed |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public static function of($source) |
34
|
|
|
{ |
35
|
|
|
return self::make($source); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Make a DataTable instance from source. |
40
|
|
|
* |
41
|
|
|
* @param mixed $source |
42
|
|
|
* @return mixed |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public static function make($source) |
46
|
|
|
{ |
47
|
|
|
$engines = config('datatables.engines'); |
48
|
|
|
$builders = config('datatables.builders'); |
49
|
|
|
|
50
|
|
|
$args = func_get_args(); |
51
|
|
|
foreach ($builders as $class => $engine) { |
52
|
|
|
if ($source instanceof $class) { |
53
|
|
|
return call_user_func_array([$engines[$engine], 'create'], $args); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($engines as $engine => $class) { |
58
|
|
|
if (call_user_func_array([$engines[$engine], 'canCreate'], $args)) { |
59
|
|
|
return call_user_func_array([$engines[$engine], 'create'], $args); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new \Exception('No available engine for ' . get_class($source)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get request object. |
68
|
|
|
* |
69
|
|
|
* @return \Yajra\DataTables\Utilities\Request |
70
|
|
|
*/ |
71
|
|
|
public function getRequest() |
72
|
|
|
{ |
73
|
|
|
return app('datatables.request'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get config instance. |
78
|
|
|
* |
79
|
|
|
* @return \Yajra\DataTables\Utilities\Config |
80
|
|
|
*/ |
81
|
|
|
public function getConfig() |
82
|
|
|
{ |
83
|
|
|
return app('datatables.config'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @deprecated Please use query() instead, this method will be removed in a next version. |
88
|
|
|
* @param $builder |
89
|
|
|
* @return QueryDataTable |
90
|
|
|
*/ |
91
|
|
|
public function queryBuilder($builder) |
92
|
|
|
{ |
93
|
|
|
return $this->query($builder); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* DataTables using Query. |
98
|
|
|
* |
99
|
|
|
* @param \Illuminate\Database\Query\Builder|mixed $builder |
100
|
|
|
* @return DataTableAbstract|QueryDataTable |
101
|
|
|
*/ |
102
|
|
|
public function query($builder) |
103
|
|
|
{ |
104
|
|
|
return QueryDataTable::create($builder); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* DataTables using Eloquent Builder. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|mixed $builder |
111
|
|
|
* @return DataTableAbstract|EloquentDataTable |
112
|
|
|
*/ |
113
|
|
|
public function eloquent($builder) |
114
|
|
|
{ |
115
|
|
|
return EloquentDataTable::create($builder); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* DataTables using Collection. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Support\Collection|array $collection |
122
|
|
|
* @return DataTableAbstract|CollectionDataTable |
123
|
|
|
*/ |
124
|
|
|
public function collection($collection) |
125
|
|
|
{ |
126
|
|
|
return CollectionDataTable::create($collection); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get html builder instance. |
131
|
|
|
* |
132
|
|
|
* @return \Yajra\DataTables\Html\Builder |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
|
|
public function getHtmlBuilder() |
136
|
|
|
{ |
137
|
|
|
if (!class_exists('\Yajra\DataTables\Html\Builder')) { |
138
|
|
|
throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->html ?: $this->html = app('datatables.html'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|