Skip to content

Commit af12776

Browse files
committed
Better support for searching without a term
1 parent dfb2320 commit af12776

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,19 @@ Search::add(Post::with('comments'), 'title')
155155
If you call the `get` method without a term or with an empty term, the package throws an `EmptySearchQueryException`. You can disable this behaviour with the `allowEmptySearchQuery` method.
156156

157157
```php
158-
Search::add(Post::with('comments'), 'title')
159-
->add(Video::with('likes'), 'title')
158+
Search::add(Post::with('comments'), 'title', 'published_at')
159+
->add(Video::with('likes'), 'title', 'released_at')
160+
->allowEmptySearchQuery()
161+
->get();
162+
```
163+
164+
In this case, you can discard the second argument as well. With the `orderBy` method, you can set the column to sort by (previously the third argument):
165+
166+
```php
167+
Search::add(Post::class)
168+
->orderBy('published_at')
169+
->add(Video::class)
170+
->orderBy('released_at')
160171
->allowEmptySearchQuery()
161172
->get();
162173
```

src/ModelToSearchThrough.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ public function __construct(Builder $builder, Collection $columns, string $order
4343
$this->key = $key;
4444
}
4545

46+
/**
47+
* Setter for the orderBy column.
48+
*
49+
* @param string $orderByColumn
50+
* @return self
51+
*/
52+
public function orderByColumn(string $orderByColumn): self
53+
{
54+
$this->orderByColumn = $orderByColumn;
55+
56+
return $this;
57+
}
58+
4659
/**
4760
* Get a cloned instance of the builder.
4861
*

src/Searcher.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function allowEmptySearchQuery(): self
120120
* @param string $orderByColumn
121121
* @return self
122122
*/
123-
public function add($query, $columns, string $orderByColumn = 'updated_at'): self
123+
public function add($query, $columns = null, string $orderByColumn = 'updated_at'): self
124124
{
125125
$modelToSearchThrough = new ModelToSearchThrough(
126126
is_string($query) ? $query::query() : $query,
@@ -134,6 +134,13 @@ public function add($query, $columns, string $orderByColumn = 'updated_at'): sel
134134
return $this;
135135
}
136136

137+
public function orderBy(string $orderByColumn): self
138+
{
139+
$this->modelsToSearchThrough->last()->orderByColumn($orderByColumn);
140+
141+
return $this;
142+
}
143+
137144
/**
138145
* Let's each search term start with a wildcard.
139146
*

tests/SearchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public function it_can_search_without_a_term()
128128
Video::create(['title' => 'bar']);
129129

130130
$results = Search::new()
131-
->add(Post::class, 'title')
132-
->add(Video::class, 'title')
131+
->add(Post::class)->orderBy('updated_at')
132+
->add(Video::class)->orderBy('published_at')
133133
->allowEmptySearchQuery()
134134
->get();
135135

0 commit comments

Comments
 (0)