@@ -318,11 +318,31 @@ private function loadResults()
318318 // The 'perPage' value is taken from the request query
319319 // string, or from the configured parameter, or it's
320320 // the first from the 'perPage' selector options.
321- $ perPage = $ this ->query ('perPage ' , $ this ->perPage ?: Arr::first ($ this ->perPageOptions ));
321+ $ defaultPerPage = $ this ->perPage ?: Arr::first ($ this ->perPageOptions );
322+
323+ $ perPage = $ this ->query ('perPage ' , $ defaultPerPage );
324+
325+ if (!in_array ($ perPage , $ this ->perPageOptions )) {
326+ // The 'perPage' value is not in the allowed options.
327+ // So we'll use the first option.
328+ $ perPage = $ defaultPerPage ;
329+ }
322330
323331 $ this ->resource = $ this ->builder ->{$ this ->paginateMethod }($ perPage )->withQueryString ();
324332 }
325333
334+ /**
335+ * Adds the given 'perPage' value to the 'perPageOptions' array.
336+ *
337+ * @return void
338+ */
339+ public function addCurrentPerPageValueToOptions ()
340+ {
341+ if ($ this ->perPage && !in_array ($ this ->perPage , $ this ->perPageOptions )) {
342+ $ this ->perPageOptions [] = $ this ->perPage ;
343+ }
344+ }
345+
326346 /**
327347 * Prepares the query and loads the results.
328348 *
@@ -334,6 +354,7 @@ public function beforeRender()
334354 $ this ->applyFilters ();
335355 $ this ->applySearchInputs ();
336356 $ this ->applySortingAndEagerLoading ();
357+ $ this ->addCurrentPerPageValueToOptions ();
337358 }
338359
339360 $ this ->loadResults ();
0 commit comments