4
4
use Illuminate \Contracts \Pagination \LengthAwarePaginator ;
5
5
use Illuminate \Database \Eloquent \Model ;
6
6
use Illuminate \Database \Eloquent \Builder ;
7
+ use Illuminate \Database \Query \Builder as QueryBuilder ;
7
8
use Illuminate \Http \JsonResponse ;
8
9
use Illuminate \Http \Request ;
9
10
use Illuminate \Support \Collection ;
@@ -21,6 +22,11 @@ class DataTableResponder
21
22
*/
22
23
private $ model ;
23
24
25
+ /**
26
+ * @var Builder
27
+ */
28
+ private $ queryBuilder ;
29
+
24
30
/**
25
31
* @var Request
26
32
*/
@@ -54,21 +60,28 @@ class DataTableResponder
54
60
/**
55
61
* DataTableResponder constructor.
56
62
*
57
- * @param $className
63
+ * @param $classNameOrQueryBuilder
58
64
* @param Request $request
59
65
*/
60
- public function __construct ($ className , Request $ request )
66
+ public function __construct ($ classNameOrQueryBuilder , Request $ request )
61
67
{
62
- if (!class_exists ($ className )) {
63
- throw new InvalidArgumentException ('Provided class does not exist. ' );
64
- }
68
+ if ($ classNameOrQueryBuilder instanceof QueryBuilder) {
69
+ $ this ->model = null ;
70
+ $ this ->queryBuilder = $ classNameOrQueryBuilder ;
71
+ } else {
72
+ if (!class_exists ($ classNameOrQueryBuilder )) {
73
+ throw new InvalidArgumentException ('Provided class does not exist. ' );
74
+ }
65
75
66
- $ this ->model = new $ className ();
67
- $ this ->request = $ request ;
76
+ $ this ->model = new $ classNameOrQueryBuilder ();
77
+ $ this ->queryBuilder = null ;
68
78
69
- if (!$ this ->model instanceof Model) {
70
- throw new InvalidArgumentException ('Provided class is not an Eloquent model. ' );
79
+ if (!$ this ->model instanceof Model) {
80
+ throw new InvalidArgumentException ('Provided class is not an Eloquent model. ' );
81
+ }
71
82
}
83
+
84
+ $ this ->request = $ request ;
72
85
}
73
86
74
87
/**
@@ -141,7 +154,7 @@ public function setResponseMeta(array $meta = [])
141
154
*/
142
155
private function buildQuery (Request $ request )
143
156
{
144
- $ query = $ this ->model ->query ();
157
+ $ query = $ this ->queryBuilder ?? $ this -> model ->query ();
145
158
146
159
$ queryManipulator = $ this ->queryManipulator ;
147
160
@@ -171,10 +184,10 @@ private function buildQuery(Request $request)
171
184
}
172
185
173
186
/**
174
- * @param Builder $query
187
+ * @param Builder|QueryBuilder $query
175
188
* @return LengthAwarePaginator
176
189
*/
177
- private function paginateQuery (Builder $ query )
190
+ private function paginateQuery ($ query )
178
191
{
179
192
return $ query ->paginate ($ this ->perPage );
180
193
}
@@ -208,11 +221,11 @@ private function manipulateCollection($results)
208
221
* `disallow_ordering_by` will always be overwritten
209
222
* as it is managed internally
210
223
*
211
- * @param Builder $query
224
+ * @param Builder|QueryBuilder $query
212
225
* @param Collection $collection
213
226
* @return array
214
227
*/
215
- private function makeMeta (Builder $ query , Collection $ collection )
228
+ private function makeMeta ($ query , Collection $ collection )
216
229
{
217
230
$ meta = $ this ->meta ;
218
231
$ out = [];
@@ -239,25 +252,28 @@ private function makeMeta(Builder $query, Collection $collection)
239
252
*/
240
253
private function disallowOrderingBy ()
241
254
{
242
- $ methods = get_class_methods ($ this ->model );
243
255
$ customAttributes = [];
244
256
245
- foreach ($ methods as $ method ) {
246
- if (!preg_match ('/^get(\w+)Attribute$/ ' , $ method , $ matches )) {
247
- continue ;
248
- }
257
+ if ($ this ->model !== null ) {
258
+ $ methods = get_class_methods ($ this ->model );
259
+
260
+ foreach ($ methods as $ method ) {
261
+ if (!preg_match ('/^get(\w+)Attribute$/ ' , $ method , $ matches )) {
262
+ continue ;
263
+ }
249
264
250
- if (empty ($ matches [1 ])) {
251
- continue ;
252
- }
265
+ if (empty ($ matches [1 ])) {
266
+ continue ;
267
+ }
253
268
254
- $ customAttribute = Str::snake ($ matches [1 ]);
269
+ $ customAttribute = Str::snake ($ matches [1 ]);
255
270
256
- if (in_array ($ customAttribute , array_keys ($ this ->orderByOverrides ))) {
257
- continue ;
258
- }
271
+ if (in_array ($ customAttribute , array_keys ($ this ->orderByOverrides ))) {
272
+ continue ;
273
+ }
259
274
260
- $ customAttributes [] = $ customAttribute ;
275
+ $ customAttributes [] = $ customAttribute ;
276
+ }
261
277
}
262
278
263
279
return $ customAttributes ;
0 commit comments