File tree Expand file tree Collapse file tree 5 files changed +54
-2
lines changed
Expand file tree Collapse file tree 5 files changed +54
-2
lines changed Original file line number Diff line number Diff line change 22
33All notable changes to ` laravel-cross-eloquent-search ` will be documented in this file
44
5+ ## 1.4.0 - 2020-10-28
6+
7+ - Allow empty search terms
8+ - Added ` new() ` method method
9+
10+ ## 1.3.1 - 2020-10-28
11+
12+ - Docs
13+
514## 1.3.0 - 2020-09-24
615
716- Support for Laravel 8.0
Original file line number Diff line number Diff line change 55use Illuminate \Support \Facades \Facade ;
66
77/*
8+ * @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher new()
89 * @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByAsc()
910 * @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher orderByDesc()
1011 * @method static \ProtoneMedia\LaravelCrossEloquentSearch\Searcher dontParseTerm()
Original file line number Diff line number Diff line change @@ -8,6 +8,16 @@ class SearchFactory
88{
99 use ForwardsCalls;
1010
11+ /**
12+ * Returns a new Searcher instance.
13+ *
14+ * @return \ProtoneMedia\LaravelCrossEloquentSearch\Searcher
15+ */
16+ public function new (): Searcher
17+ {
18+ return new Searcher ;
19+ }
20+
1121 /**
1222 * Handle dynamic method calls into a new Searcher instance.
1323 *
@@ -18,7 +28,7 @@ class SearchFactory
1828 public function __call ($ method , $ parameters )
1929 {
2030 return $ this ->forwardCallTo (
21- new Searcher ,
31+ $ this -> new () ,
2232 $ method ,
2333 $ parameters
2434 );
Original file line number Diff line number Diff line change @@ -26,6 +26,11 @@ class Searcher
2626 */
2727 private bool $ startWithWildcard = false ;
2828
29+ /**
30+ * Allow an empty search query.
31+ */
32+ private bool $ allowEmptySearchQuery = false ;
33+
2934 /**
3035 * Collection of search terms.
3136 */
@@ -97,6 +102,16 @@ public function dontParseTerm(): self
97102 return $ this ;
98103 }
99104
105+ /**
106+ * Allow empty search terms.
107+ */
108+ public function allowEmptySearchQuery (): self
109+ {
110+ $ this ->allowEmptySearchQuery = true ;
111+
112+ return $ this ;
113+ }
114+
100115 /**
101116 * Add a model to search through.
102117 *
@@ -180,7 +195,7 @@ private function initializeTerms(string $terms): self
180195 ->filter ()
181196 ->map (fn ($ term ) => ($ this ->startWithWildcard ? '% ' : '' ) . "{$ term }% " );
182197
183- if ($ this ->terms ->isEmpty ()) {
198+ if (! $ this -> allowEmptySearchQuery && $ this ->terms ->isEmpty ()) {
184199 throw new EmptySearchQueryException ;
185200 }
186201
Original file line number Diff line number Diff line change @@ -119,6 +119,23 @@ public function it_throws_an_exception_when_the_query_is_empty()
119119 $ this ->fail ('Should have thrown EmptySearchQueryException. ' );
120120 }
121121
122+ /** @test */
123+ public function it_can_search_without_a_term ()
124+ {
125+ Post::create (['title ' => 'foo ' ]);
126+ Post::create (['title ' => 'bar ' ]);
127+ Video::create (['title ' => 'foo ' ]);
128+ Video::create (['title ' => 'bar ' ]);
129+
130+ $ results = Search::new ()
131+ ->add (Post::class, 'title ' )
132+ ->add (Video::class, 'title ' )
133+ ->allowEmptySearchQuery ()
134+ ->get ();
135+
136+ $ this ->assertCount (4 , $ results );
137+ }
138+
122139 /** @test */
123140 public function it_can_search_on_the_left_side_of_the_term ()
124141 {
You can’t perform that action at this time.
0 commit comments