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