|
4 | 4 | use Illuminate\Database\Eloquent\Model;
|
5 | 5 | use Illuminate\Database\Eloquent\Builder;
|
6 | 6 | use Illuminate\Http\Request;
|
| 7 | +use Illuminate\Support\Collection; |
7 | 8 | use Illuminate\Support\Str;
|
8 | 9 |
|
9 | 10 | /**
|
@@ -42,6 +43,11 @@ class DataTableResponder
|
42 | 43 | */
|
43 | 44 | private $perPage = 15;
|
44 | 45 |
|
| 46 | + /** |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + private $meta = []; |
| 50 | + |
45 | 51 | /**
|
46 | 52 | * DataTableResponder constructor.
|
47 | 53 | *
|
@@ -110,6 +116,20 @@ public function collectionManipulator(callable $collectionManipulator)
|
110 | 116 | return $this;
|
111 | 117 | }
|
112 | 118 |
|
| 119 | + /** |
| 120 | + * Sets the meta for the API response |
| 121 | + * |
| 122 | + * @see DataTableResponder::makeMeta |
| 123 | + * |
| 124 | + * @param callable $collectionManipulator |
| 125 | + * @return DataTableResponder |
| 126 | + */ |
| 127 | + public function setResponseMeta(array $meta = []) |
| 128 | + { |
| 129 | + $this->meta = $meta; |
| 130 | + return $this; |
| 131 | + } |
| 132 | + |
113 | 133 | /**
|
114 | 134 | * Builds the Eloquent query based on the request.
|
115 | 135 | *
|
@@ -172,6 +192,41 @@ private function manipulateCollection($results)
|
172 | 192 | return $results;
|
173 | 193 | }
|
174 | 194 |
|
| 195 | + /** |
| 196 | + * Make response meta |
| 197 | + * |
| 198 | + * If a callable is given as an element value then |
| 199 | + * the query and collection as parameters |
| 200 | + * |
| 201 | + * `disallow_ordering_by` will always be overwritten |
| 202 | + * as it is managed internally |
| 203 | + * |
| 204 | + * @param Builder $query |
| 205 | + * @param Collection $collection |
| 206 | + * @return array |
| 207 | + */ |
| 208 | + private function makeMeta(Builder $query, Collection $collection) |
| 209 | + { |
| 210 | + $meta = $this->meta; |
| 211 | + $out = []; |
| 212 | + |
| 213 | + foreach($meta as $element => $value) { |
| 214 | + if (is_callable($value)) { |
| 215 | + $out[$element] = call_user_func_array( |
| 216 | + $value, [$query, $collection] |
| 217 | + ); |
| 218 | + |
| 219 | + continue; |
| 220 | + } |
| 221 | + |
| 222 | + $out[$element] = $value; |
| 223 | + } |
| 224 | + |
| 225 | + $out['disallow_ordering_by'] = $this->disallowOrderingBy(); |
| 226 | + |
| 227 | + return $out; |
| 228 | + } |
| 229 | + |
175 | 230 | /**
|
176 | 231 | * @return array|string[]
|
177 | 232 | */
|
@@ -210,9 +265,8 @@ public function respond()
|
210 | 265 |
|
211 | 266 | $results = $this->paginateQuery($query);
|
212 | 267 | $results = $this->manipulateCollection($results);
|
| 268 | + $meta = $this->makeMeta($query, $results->getCollection()); |
213 | 269 |
|
214 |
| - $disallowOrderingBy = $this->disallowOrderingBy(); |
215 |
| - |
216 |
| - return DataTableResponse::success($results, ['disallow_ordering_by' => $disallowOrderingBy])->json(); |
| 270 | + return DataTableResponse::success($results, $meta)->json(); |
217 | 271 | }
|
218 | 272 | }
|
0 commit comments