@@ -15,49 +15,54 @@ class Searcher
1515 /**
1616 * Collection of models to search through.
1717 */
18- private Collection $ modelsToSearchThrough ;
18+ protected Collection $ modelsToSearchThrough ;
1919
2020 /**
2121 * Sort direction.
2222 */
23- private string $ orderByDirection ;
23+ protected string $ orderByDirection ;
2424
2525 /**
2626 * Start the search term with a wildcard.
2727 */
28- private bool $ startWithWildcard = false ;
28+ protected bool $ startWithWildcard = false ;
2929
3030 /**
3131 * Allow an empty search query.
3232 */
33- private bool $ allowEmptySearchQuery = false ;
33+ protected bool $ allowEmptySearchQuery = false ;
3434
3535 /**
3636 * Collection of search terms.
3737 */
38- private Collection $ terms ;
38+ protected Collection $ terms ;
3939
4040 /**
4141 * The number of items to be shown per page.
4242 */
43- private int $ perPage = 15 ;
43+ protected int $ perPage = 15 ;
4444
4545 /**
4646 * The query string variable used to store the page.
4747 */
48- private string $ pageName = 'page ' ;
48+ protected string $ pageName = 'page ' ;
4949
5050 /**
5151 * Parse the search term into multiple terms.
5252 */
53- private bool $ parseTerm = true ;
53+ protected bool $ parseTerm = true ;
54+
55+ /**
56+ * Use simplePaginate() on Eloquent\Builder vs paginate()
57+ */
58+ protected bool $ simplePaginate = false ;
5459
5560 /**
5661 * Current page.
5762 *
5863 * @var int|null
5964 */
60- private $ page ;
65+ protected $ page ;
6166
6267 /**
6368 * Initialises the instanace with a fresh Collection and default sort.
@@ -170,9 +175,27 @@ public function startWithWildcard(): self
170175 */
171176 public function paginate ($ perPage = 15 , $ pageName = 'page ' , $ page = null ): self
172177 {
173- $ this ->page = $ page ?: Paginator::resolveCurrentPage ($ pageName );
174- $ this ->pageName = $ pageName ;
175- $ this ->perPage = $ perPage ;
178+ $ this ->page = $ page ?: Paginator::resolveCurrentPage ($ pageName );
179+ $ this ->pageName = $ pageName ;
180+ $ this ->perPage = $ perPage ;
181+ $ this ->simplePaginate = false ;
182+
183+ return $ this ;
184+ }
185+
186+ /**
187+ * Paginate using simple pagination.
188+ *
189+ * @param integer $perPage
190+ * @param string $pageName
191+ * @param int|null $page
192+ * @return self
193+ */
194+ public function simplePaginate ($ perPage = 15 , $ pageName = 'page ' , $ page = null ): self
195+ {
196+ $ this ->paginate ($ perPage , $ pageName , $ page );
197+
198+ $ this ->simplePaginate = true ;
176199
177200 return $ this ;
178201 }
@@ -201,7 +224,7 @@ public function parseTerms(string $terms, callable $callback = null): Collection
201224 * @throws \ProtoneMedia\LaravelCrossEloquentSearch\EmptySearchQueryException
202225 * @return self
203226 */
204- private function initializeTerms (string $ terms ): self
227+ protected function initializeTerms (string $ terms ): self
205228 {
206229 $ terms = $ this ->parseTerm ? $ this ->parseTerms ($ terms ) : $ terms ;
207230
@@ -241,7 +264,7 @@ public function addSearchQueryToBuilder(Builder $builder, ModelToSearchThrough $
241264 * @param \ProtoneMedia\LaravelCrossEloquentSearch\ModelToSearchThrough $currentModel
242265 * @return array
243266 */
244- private function makeSelects (ModelToSearchThrough $ currentModel ): array
267+ protected function makeSelects (ModelToSearchThrough $ currentModel ): array
245268 {
246269 return $ this ->modelsToSearchThrough ->flatMap (function (ModelToSearchThrough $ modelToSearchThrough ) use ($ currentModel ) {
247270 $ qualifiedKeyName = $ qualifiedOrderByColumnName = 'null ' ;
@@ -264,7 +287,7 @@ private function makeSelects(ModelToSearchThrough $currentModel): array
264287 *
265288 * @return string
266289 */
267- private function makeOrderBy (): string
290+ protected function makeOrderBy (): string
268291 {
269292 $ modelOrderKeys = $ this ->modelsToSearchThrough ->map ->getModelKey ('order ' )->implode (', ' );
270293
@@ -276,7 +299,7 @@ private function makeOrderBy(): string
276299 *
277300 * @return \Illuminate\Support\Collection
278301 */
279- private function buildQueries (): Collection
302+ protected function buildQueries (): Collection
280303 {
281304 return $ this ->modelsToSearchThrough ->map (function (ModelToSearchThrough $ modelToSearchThrough ) {
282305 return $ modelToSearchThrough ->getFreshBuilder ()
@@ -293,7 +316,7 @@ private function buildQueries(): Collection
293316 *
294317 * @return
295318 */
296- private function getCompiledQueryBuilder (): QueryBuilder
319+ protected function getCompiledQueryBuilder (): QueryBuilder
297320 {
298321 $ queries = $ this ->buildQueries ();
299322
@@ -312,13 +335,16 @@ private function getCompiledQueryBuilder(): QueryBuilder
312335 *
313336 * @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
314337 */
315- private function getIdAndOrderAttributes ()
338+ protected function getIdAndOrderAttributes ()
316339 {
317340 $ query = $ this ->getCompiledQueryBuilder ();
318341
342+ // Determine the pagination method to call on Eloquent\Builder
343+ $ paginateMethod = $ this ->simplePaginate ? 'simplePaginate ' : 'paginate ' ;
344+
319345 // get all results or limit the results by pagination
320346 return $ this ->perPage
321- ? $ query ->paginate ($ this ->perPage , ['* ' ], $ this ->pageName , $ this ->page )
347+ ? $ query ->{ $ paginateMethod } ($ this ->perPage , ['* ' ], $ this ->pageName , $ this ->page )
322348 : $ query ->get ();
323349
324350 // the collection will be something like:
@@ -345,7 +371,7 @@ private function getIdAndOrderAttributes()
345371 * @param \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator $results
346372 * @return \Illuminate\Support\Collection
347373 */
348- private function getModelsPerType ($ results )
374+ protected function getModelsPerType ($ results )
349375 {
350376 return $ this ->modelsToSearchThrough
351377 ->keyBy ->getModelKey ()
0 commit comments