@@ -47,6 +47,13 @@ class Builder
47
47
*/
48
48
public $ queryCallback ;
49
49
50
+ /**
51
+ * Optional callback after raw search.
52
+ *
53
+ * @var \Closure|null
54
+ */
55
+ public $ afterRawSearchCallback ;
56
+
50
57
/**
51
58
* The custom index specified for the search.
52
59
*
@@ -310,6 +317,19 @@ public function raw()
310
317
return $ this ->engine ()->search ($ this );
311
318
}
312
319
320
+ /**
321
+ * Set the callback that should have an opportunity to inspect and modify the raw result returned by the search engine.
322
+ *
323
+ * @param callable $callback
324
+ * @return $this
325
+ */
326
+ public function withRawResults ($ callback )
327
+ {
328
+ $ this ->afterRawSearchCallback = $ callback ;
329
+
330
+ return $ this ;
331
+ }
332
+
313
333
/**
314
334
* Get the keys of search results.
315
335
*
@@ -373,7 +393,9 @@ public function simplePaginate($perPage = null, $pageName = 'page', $page = null
373
393
$ perPage = $ perPage ?: $ this ->model ->getPerPage ();
374
394
375
395
$ results = $ this ->model ->newCollection ($ engine ->map (
376
- $ this , $ rawResults = $ engine ->paginate ($ this , $ perPage , $ page ), $ this ->model
396
+ $ this ,
397
+ $ this ->applyAfterRawSearchCallback ($ rawResults = $ engine ->paginate ($ this , $ perPage , $ page )),
398
+ $ this ->model
377
399
)->all ());
378
400
379
401
$ paginator = Container::getInstance ()->makeWith (Paginator::class, [
@@ -411,7 +433,7 @@ public function simplePaginateRaw($perPage = null, $pageName = 'page', $page = n
411
433
412
434
$ perPage = $ perPage ?: $ this ->model ->getPerPage ();
413
435
414
- $ results = $ engine ->paginate ($ this , $ perPage , $ page );
436
+ $ results = $ this -> applyAfterRawSearchCallback ( $ engine ->paginate ($ this , $ perPage , $ page) );
415
437
416
438
$ paginator = Container::getInstance ()->makeWith (Paginator::class, [
417
439
'items ' => $ results ,
@@ -449,7 +471,9 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
449
471
$ perPage = $ perPage ?: $ this ->model ->getPerPage ();
450
472
451
473
$ results = $ this ->model ->newCollection ($ engine ->map (
452
- $ this , $ rawResults = $ engine ->paginate ($ this , $ perPage , $ page ), $ this ->model
474
+ $ this ,
475
+ $ this ->applyAfterRawSearchCallback ($ rawResults = $ engine ->paginate ($ this , $ perPage , $ page )),
476
+ $ this ->model
453
477
)->all ());
454
478
455
479
return Container::getInstance ()->makeWith (LengthAwarePaginator::class, [
@@ -486,7 +510,7 @@ public function paginateRaw($perPage = null, $pageName = 'page', $page = null)
486
510
487
511
$ perPage = $ perPage ?: $ this ->model ->getPerPage ();
488
512
489
- $ results = $ engine ->paginate ($ this , $ perPage , $ page );
513
+ $ results = $ this -> applyAfterRawSearchCallback ( $ engine ->paginate ($ this , $ perPage , $ page) );
490
514
491
515
return Container::getInstance ()->makeWith (LengthAwarePaginator::class, [
492
516
'items ' => $ results ,
@@ -531,6 +555,21 @@ protected function getTotalCount($results)
531
555
)->toBase ()->getCountForPagination ();
532
556
}
533
557
558
+ /**
559
+ * Invoke the "after raw search" callback.
560
+ *
561
+ * @param mixed $results
562
+ * @return mixed
563
+ */
564
+ public function applyAfterRawSearchCallback ($ results )
565
+ {
566
+ if ($ this ->afterRawSearchCallback ) {
567
+ call_user_func ($ this ->afterRawSearchCallback , $ results );
568
+ }
569
+
570
+ return $ results ;
571
+ }
572
+
534
573
/**
535
574
* Get the engine that should handle the query.
536
575
*
0 commit comments