@@ -18,6 +18,13 @@ class ListingResponseBuilder
1818 */
1919 protected array $ fields ;
2020
21+ /**
22+ * Which fields are filterable.
23+ * When null, the $fields above are used instead (Allow all fields).
24+ * @var string[]|null
25+ */
26+ protected array |null $ filterableFields = null ;
27+
2128 /**
2229 * @var array<callable>
2330 */
@@ -54,7 +61,7 @@ public function toResponse(): JsonResponse
5461 {
5562 $ filteredQuery = $ this ->filterQuery ($ this ->query );
5663
57- $ total = $ filteredQuery ->count ();
64+ $ total = $ filteredQuery ->getCountForPagination ();
5865 $ data = $ this ->fetchData ($ filteredQuery )->each (function ($ model ) {
5966 foreach ($ this ->resultModifiers as $ modifier ) {
6067 $ modifier ($ model );
@@ -77,6 +84,14 @@ public function modifyResults(callable $modifier): void
7784 $ this ->resultModifiers [] = $ modifier ;
7885 }
7986
87+ /**
88+ * Limit filtering to just the given set of fields.
89+ */
90+ public function setFilterableFields (array $ fields ): void
91+ {
92+ $ this ->filterableFields = $ fields ;
93+ }
94+
8095 /**
8196 * Fetch the data to return within the response.
8297 */
@@ -94,7 +109,7 @@ protected function fetchData(Builder $query): Collection
94109 protected function filterQuery (Builder $ query ): Builder
95110 {
96111 $ query = clone $ query ;
97- $ requestFilters = $ this ->request ->get ('filter ' , []);
112+ $ requestFilters = $ this ->request ->input ('filter ' , []);
98113 if (!is_array ($ requestFilters )) {
99114 return $ query ;
100115 }
@@ -114,10 +129,11 @@ protected function filterQuery(Builder $query): Builder
114129 protected function requestFilterToQueryFilter ($ fieldKey , $ value ): ?array
115130 {
116131 $ splitKey = explode (': ' , $ fieldKey );
117- $ field = $ splitKey [0 ];
132+ $ field = strtolower ( $ splitKey [0 ]) ;
118133 $ filterOperator = $ splitKey [1 ] ?? 'eq ' ;
119134
120- if (!in_array ($ field , $ this ->fields )) {
135+ $ filterFields = $ this ->filterableFields ?? $ this ->fields ;
136+ if (!in_array ($ field , $ filterFields )) {
121137 return null ;
122138 }
123139
@@ -140,8 +156,8 @@ protected function sortQuery(Builder $query): Builder
140156 $ defaultSortName = $ this ->fields [0 ];
141157 $ direction = 'asc ' ;
142158
143- $ sort = $ this ->request ->get ('sort ' , '' );
144- if (strpos ($ sort , '- ' ) === 0 ) {
159+ $ sort = $ this ->request ->input ('sort ' , '' );
160+ if (str_starts_with ($ sort , '- ' )) {
145161 $ direction = 'desc ' ;
146162 }
147163
@@ -160,9 +176,9 @@ protected function sortQuery(Builder $query): Builder
160176 protected function countAndOffsetQuery (Builder $ query ): Builder
161177 {
162178 $ query = clone $ query ;
163- $ offset = max (0 , $ this ->request ->get ('offset ' , 0 ));
179+ $ offset = max (0 , $ this ->request ->input ('offset ' , 0 ));
164180 $ maxCount = config ('api.max_item_count ' );
165- $ count = $ this ->request ->get ('count ' , config ('api.default_item_count ' ));
181+ $ count = $ this ->request ->input ('count ' , config ('api.default_item_count ' ));
166182 $ count = max (min ($ maxCount , $ count ), 1 );
167183
168184 return $ query ->skip ($ offset )->take ($ count );
0 commit comments