Skip to content

Commit 9e7ffa7

Browse files
authored
Merge pull request #2 from dextermb/feature/collection-manipulator
Feature/collection manipulator
2 parents d54bc79 + 0a8e8f4 commit 9e7ffa7

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ composer require langleyfoxall/react-dynamic-data-table-laravel-api
1414

1515
First, create a new route in your API routes file for the data table response, and point it to a controller.
1616

17-
In this controller method, create a new `DataTableResponder` passing it the model you wish to return data about, and the provided instance of the `Request` object. You can optionally specify changes to the query (such as sorting, or filtering) using the `query` method. You can also change number of records shown per page with the `setPerPage` method.
17+
In this controller method, create a new `DataTableResponder` passing it the model you wish to return data about, and the provided instance of the `Request` object. You can optionally specify changes to the query (such as sorting, or filtering) using the `query` method. If you want to alter the data before it gets returned in some way, such as changing attribute values or appending custom attributes, you can take advantage of `collectionManipulator`. You can also change number of records shown per page with the `setPerPage` method.
1818

1919
See the example usage below.
2020

@@ -28,10 +28,15 @@ class UsersController extends Controller
2828
public function dataTable(Request $request)
2929
{
3030
return (new DataTableResponder(User::class, $request))
31-
->query(function($query) { // Optional, default: none
31+
->query(function($query) { // Optional, default: none
3232
$query->where('name', 'like', 'B%');
3333
})
34-
->setPerPage(10) // Optional, default: 15
34+
->collectionManipulator(function (Collection $collection) { // Optional, default: none
35+
$collection->map(function($user) {
36+
$user->name = title_case($user->name);
37+
});
38+
})
39+
->setPerPage(10) // Optional, default: 15
3540
->respond();
3641
}
3742
}

src/DataTableResponder.php

+39
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class DataTableResponder
2626
*/
2727
private $queryManipulator;
2828

29+
/**
30+
* @var callable
31+
*/
32+
private $collectionManipulator;
33+
2934
/**
3035
* @var int
3136
*/
@@ -75,6 +80,18 @@ public function query(callable $queryManipulator)
7580
return $this;
7681
}
7782

83+
/**
84+
* Sets the callable used to manipulate the query results collection
85+
*
86+
* @param callable $collectionManipulator
87+
* @return DataTableResponder
88+
*/
89+
public function collectionManipulator(callable $collectionManipulator)
90+
{
91+
$this->collectionManipulator = $collectionManipulator;
92+
return $this;
93+
}
94+
7895
/**
7996
* Builds the Eloquent query based on the request.
8097
*
@@ -109,13 +126,35 @@ private function paginateQuery(Builder $query)
109126
return $query->paginate($this->perPage);
110127
}
111128

129+
/**
130+
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $results
131+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
132+
*/
133+
private function manipulateCollection($results)
134+
{
135+
$collection = $results->getCollection();
136+
$manipulator = $this->collectionManipulator;
137+
138+
if ($manipulator) {
139+
$manipulated = $manipulator($collection);
140+
141+
if ($manipulated) {
142+
$results->setCollection($manipulated);
143+
}
144+
}
145+
146+
return $results;
147+
}
148+
112149
/**
113150
* @return \Illuminate\Http\JsonResponse
114151
*/
115152
public function respond()
116153
{
117154
$query = $this->buildQuery($this->request);
155+
118156
$results = $this->paginateQuery($query);
157+
$results = $this->manipulateCollection($results);
119158

120159
return DataTableResponse::success($results)->json();
121160
}

0 commit comments

Comments
 (0)