44
55use Illuminate \Database \Eloquent \Builder ;
66use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
7+ use Illuminate \Database \Eloquent \Model ;
78use Illuminate \Database \Query \Builder as BaseBuilder ;
89use Illuminate \Database \Query \Builder as QueryBuilder ;
910use Illuminate \Database \Query \Grammars \MySqlGrammar ;
1213use Illuminate \Support \Collection ;
1314use Illuminate \Support \Facades \DB ;
1415use Illuminate \Support \Str ;
15- use Illuminate \Database \Eloquent \Model ;
1616
1717class Searcher
1818{
@@ -56,6 +56,11 @@ class Searcher
5656 */
5757 protected bool $ ignoreCase = false ;
5858
59+ /**
60+ * Raw input.
61+ */
62+ protected ?string $ rawTerms = null ;
63+
5964 /**
6065 * Collection of search terms.
6166 */
@@ -185,6 +190,7 @@ public function includeModelType(string $key = 'type'): self
185190 * @param \Illuminate\Database\Eloquent\Builder|string $query
186191 * @param string|array|\Illuminate\Support\Collection $columns
187192 * @param string $orderByColumn
193+ * @param bool $fullText
188194 * @return self
189195 */
190196 public function add ($ query , $ columns = null , string $ orderByColumn = null ): self
@@ -195,7 +201,25 @@ public function add($query, $columns = null, string $orderByColumn = null): self
195201 $ builder ,
196202 Collection::wrap ($ columns ),
197203 $ orderByColumn ?: $ builder ->getModel ()->getUpdatedAtColumn (),
198- $ this ->modelsToSearchThrough ->count ()
204+ $ this ->modelsToSearchThrough ->count (),
205+ );
206+
207+ $ this ->modelsToSearchThrough ->push ($ modelToSearchThrough );
208+
209+ return $ this ;
210+ }
211+
212+ public function addFullText ($ query , $ columns = null , array $ options = [], string $ orderByColumn = null ): self
213+ {
214+ $ builder = is_string ($ query ) ? $ query ::query () : $ query ;
215+
216+ $ modelToSearchThrough = new ModelToSearchThrough (
217+ $ builder ,
218+ Collection::wrap ($ columns ),
219+ $ orderByColumn ?: $ builder ->getModel ()->getUpdatedAtColumn (),
220+ $ this ->modelsToSearchThrough ->count (),
221+ true ,
222+ $ options
199223 );
200224
201225 $ this ->modelsToSearchThrough ->push ($ modelToSearchThrough );
@@ -363,6 +387,8 @@ public function parseTerms(string $terms, callable $callback = null): Collection
363387 */
364388 protected function initializeTerms (string $ terms ): self
365389 {
390+ $ this ->rawTerms = $ terms ;
391+
366392 $ terms = $ this ->parseTerm ? $ this ->parseTerms ($ terms ) : $ terms ;
367393
368394 $ this ->termsWithoutWildcards = Collection::wrap ($ terms )->filter ()->map (function ($ term ) {
@@ -398,10 +424,19 @@ public function addSearchQueryToBuilder(Builder $builder, ModelToSearchThrough $
398424 }
399425
400426 $ builder ->where (function (Builder $ query ) use ($ modelToSearchThrough ) {
427+ if ($ modelToSearchThrough ->searchFullText ()) {
428+ return $ this ->addWhereTermsToQuery (
429+ $ query ,
430+ $ modelToSearchThrough ->getColumns ()->map (fn ($ column ) => $ modelToSearchThrough ->qualifyColumn ($ column ))->all (),
431+ true ,
432+ $ modelToSearchThrough ->fullTextOptions ()
433+ );
434+ }
435+
401436 $ modelToSearchThrough ->getColumns ()->each (function ($ column ) use ($ query , $ modelToSearchThrough ) {
402437 Str::contains ($ column , '. ' )
403- ? $ this ->addNestedRelationToQuery ($ query , $ column )
404- : $ this ->addWhereTermsToQuery ($ query , $ modelToSearchThrough ->qualifyColumn ($ column ));
438+ ? $ this ->addNestedRelationToQuery ($ query , $ column, $ modelToSearchThrough -> searchFullText () )
439+ : $ this ->addWhereTermsToQuery ($ query , $ modelToSearchThrough ->qualifyColumn ($ column ), $ modelToSearchThrough -> searchFullText () );
405440 });
406441 });
407442 }
@@ -432,11 +467,17 @@ private function addNestedRelationToQuery(Builder $query, string $nestedRelation
432467 * Adds an 'orWhere' clause to search for each term in the given column.
433468 *
434469 * @param \Illuminate\Database\Eloquent\Builder $builder
435- * @param string $column
470+ * @param array|string $columns
471+ * @param bool $fullText
472+ * @param array $fullTextOptions
436473 * @return void
437474 */
438- private function addWhereTermsToQuery (Builder $ query , string $ column )
475+ private function addWhereTermsToQuery (Builder $ query , $ column, bool $ fullText = false , array $ fullTextOptions = [] )
439476 {
477+ if ($ fullText ) {
478+ return $ query ->orWhereFullText ($ column , $ this ->rawTerms , $ fullTextOptions );
479+ }
480+
440481 $ column = $ this ->ignoreCase ? (new MySqlGrammar )->wrap ($ column ) : $ column ;
441482
442483 $ this ->terms ->each (function ($ term ) use ($ query , $ column ) {
0 commit comments