4
4
use Illuminate \Database \Eloquent \Model ;
5
5
use Illuminate \Database \Eloquent \Builder ;
6
6
use Illuminate \Http \Request ;
7
+ use Illuminate \Support \Str ;
7
8
8
9
/**
9
10
* Class DataTableResponder
@@ -26,6 +27,11 @@ class DataTableResponder
26
27
*/
27
28
private $ queryManipulator ;
28
29
30
+ /**
31
+ * @var array|callable[]
32
+ */
33
+ private $ orderByOverrides = [];
34
+
29
35
/**
30
36
* @var callable
31
37
*/
@@ -80,9 +86,21 @@ public function query(callable $queryManipulator)
80
86
return $ this ;
81
87
}
82
88
89
+ /**
90
+ * Sets the field name to callable mapping array used to override the query order by logic
91
+ *
92
+ * @param array|callable[] $orderByOverride
93
+ * @return DataTableResponder
94
+ */
95
+ public function overrideOrderByLogic (array $ orderByOverrides )
96
+ {
97
+ $ this ->orderByOverrides = $ orderByOverrides ;
98
+ return $ this ;
99
+ }
100
+
83
101
/**
84
102
* Sets the callable used to manipulate the query results collection
85
- *
103
+ *
86
104
* @param callable $collectionManipulator
87
105
* @return DataTableResponder
88
106
*/
@@ -106,10 +124,18 @@ private function buildQuery(Request $request)
106
124
$ query = $ this ->model ->query ();
107
125
108
126
if ($ orderByField && $ orderByDirection ) {
109
- $ query ->orderBy ($ orderByField , $ orderByDirection );
127
+ if (in_array ($ orderByField , array_keys ($ this ->orderByOverrides ))) {
128
+ call_user_func_array (
129
+ $ this ->orderByOverrides [$ orderByField ],
130
+ [$ query , $ orderByDirection ]
131
+ );
132
+ } else {
133
+ $ query ->orderBy ($ orderByField , $ orderByDirection );
134
+ }
110
135
}
111
136
112
137
$ queryManipulator = $ this ->queryManipulator ;
138
+
113
139
if ($ queryManipulator ) {
114
140
$ queryManipulator ($ query );
115
141
}
@@ -146,6 +172,35 @@ private function manipulateCollection($results)
146
172
return $ results ;
147
173
}
148
174
175
+ /**
176
+ * @return array|string[]
177
+ */
178
+ private function disallowOrderingBy ()
179
+ {
180
+ $ methods = get_class_methods ($ this ->model );
181
+ $ customAttributes = [];
182
+
183
+ foreach ($ methods as $ method ) {
184
+ if (!preg_match ('/^get(\w+)Attribute$/ ' , $ method , $ matches )) {
185
+ continue ;
186
+ }
187
+
188
+ if (empty ($ matches [1 ])) {
189
+ continue ;
190
+ }
191
+
192
+ $ customAttribute = Str::snake ($ matches [1 ]);
193
+
194
+ if (in_array ($ customAttribute , array_keys ($ this ->orderByOverrides ))) {
195
+ continue ;
196
+ }
197
+
198
+ $ customAttributes [] = $ customAttribute ;
199
+ }
200
+
201
+ return $ customAttributes ;
202
+ }
203
+
149
204
/**
150
205
* @return \Illuminate\Http\JsonResponse
151
206
*/
@@ -156,6 +211,8 @@ public function respond()
156
211
$ results = $ this ->paginateQuery ($ query );
157
212
$ results = $ this ->manipulateCollection ($ results );
158
213
159
- return DataTableResponse::success ($ results )->json ();
214
+ $ disallowOrderingBy = $ this ->disallowOrderingBy ();
215
+
216
+ return DataTableResponse::success ($ results , ['disallow_ordering_by ' => $ disallowOrderingBy ])->json ();
160
217
}
161
218
}
0 commit comments