Skip to content

Commit 3d308b5

Browse files
authored
Support for addMany and addWhen methods (protonemedia#12)
1 parent 0e0a122 commit 3d308b5

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-cross-eloquent-search` will be documented in this file
44

5+
## 1.9.0 - 2020-12-23
6+
7+
- Support for `addMany` and `andWhen` methods.
8+
59
## 1.8.0 - 2020-12-23
610

711
- Support for simple pagination (credit to @mewejo).

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ Search::new()
6363
->get('howto');
6464
```
6565

66+
You can add multiple models at once by using the `addMany` method:
67+
68+
```php
69+
Search::addMany([
70+
[Post::class, 'title'],
71+
[Video::class, 'title'],
72+
])->get('howto');
73+
```
74+
75+
There's also an `addWhen` method, that adds the model when the first argument given to the method evaluates to `true`:
76+
77+
```php
78+
Search::new()
79+
->addWhen($user, Post::class, 'title')
80+
->addWhen($user->isAdmin(), Video::class, 'title')
81+
->get('howto');
82+
```
83+
6684
### Sorting
6785

6886
If you want to sort the results by another column, you can pass that column to the `add` method as a third parameter. Call the `orderByDesc` method to sort the results in descending order.

src/Searcher.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,39 @@ public function add($query, $columns = null, string $orderByColumn = 'updated_at
140140
return $this;
141141
}
142142

143+
/**
144+
* Apply the model if the value is truthy.
145+
*
146+
* @param mixed $value
147+
* @param \Illuminate\Database\Eloquent\Builder|string $query
148+
* @param string|array|\Illuminate\Support\Collection $columns
149+
* @param string $orderByColumn
150+
* @return self
151+
*/
152+
public function addWhen($value, $query, $columns = null, string $orderByColumn = 'updated_at'): self
153+
{
154+
if (!$value) {
155+
return $this;
156+
}
157+
158+
return $this->add($query, $columns, $orderByColumn);
159+
}
160+
161+
/**
162+
* Loop through the queries and add them.
163+
*
164+
* @param mixed $value
165+
* @return self
166+
*/
167+
public function addMany($queries): self
168+
{
169+
Collection::make($queries)->each(function ($query) {
170+
$this->add(...$query);
171+
});
172+
173+
return $this;
174+
}
175+
143176
/**
144177
* Set the 'orderBy' column of the latest added model.
145178
*

tests/SearchTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,40 @@ public function it_can_search_without_a_term()
152152
$this->assertCount(4, $results);
153153
}
154154

155+
/** @test */
156+
public function it_can_conditionally_add_queries()
157+
{
158+
$postA = Post::create(['title' => 'foo']);
159+
Post::create(['title' => 'bar']);
160+
Video::create(['title' => 'foo']);
161+
Video::create(['title' => 'bar']);
162+
163+
$results = Search::new()
164+
->addWhen(true, Post::class, 'title')
165+
->addWhen(false, Video::class, 'title', 'published_at')
166+
->get('foo');
167+
168+
$this->assertCount(1, $results);
169+
$this->assertTrue($results->first()->is($postA));
170+
}
171+
172+
/** @test */
173+
public function it_can_add_many_models_at_once()
174+
{
175+
$videoA = Video::create(['title' => 'foo']);
176+
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);
177+
178+
$results = Search::addMany([
179+
[Video::class, 'title'],
180+
[Video::class, 'subtitle', 'created_at'],
181+
])->get('foo');
182+
183+
$this->assertCount(2, $results);
184+
185+
$this->assertTrue($results->contains($videoA));
186+
$this->assertTrue($results->contains($videoB));
187+
}
188+
155189
/** @test */
156190
public function it_can_search_on_the_left_side_of_the_term()
157191
{

0 commit comments

Comments
 (0)