Skip to content

Commit 3dbb04c

Browse files
committed
Eager load
1 parent 689e574 commit 3dbb04c

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ $results = Search::new()
7676
->get('foo');
7777
```
7878

79+
### Add wildcard on left side of the term
80+
81+
```php
82+
$results = Search::new()
83+
->add(Post::class, 'title')
84+
->add(Video::class, 'title')
85+
->wildcardLeft()
86+
->get('foo');
87+
```
88+
89+
### Eager load relations
90+
91+
```php
92+
$results = Search::new()
93+
->add(Post::with('comments'), 'title')
94+
->add(Video::with('likes'), 'title')
95+
->get('foo');
96+
```
97+
7998
### Testing
8099

81100
``` bash

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
"illuminate/support": "^6.0|^7.0"
2121
},
2222
"require-dev": {
23-
"mockery/mockery": "^1.3",
2423
"orchestra/testbench": "^4.0|^5.0",
25-
"phpunit/phpunit": "^8.0"
24+
"phpunit/phpunit": "^8.5"
2625
},
2726
"autoload": {
2827
"psr-4": {

src/PendingQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getFreshBuilder(): Builder
3434

3535
public function newQueryWithoutScopes(): Builder
3636
{
37-
return $this->getModel()->newQueryWithoutScopes();
37+
return $this->getFreshBuilder();
3838
}
3939

4040
public function getQualifiedColumns(): Collection

tests/SearchTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,23 @@ public function it_can_paginate_the_results()
160160
$this->assertTrue($resultsPage2->first()->is($postA));
161161
$this->assertTrue($resultsPage2->last()->is($videoA));
162162
}
163+
164+
/** @test */
165+
public function it_can_eager_load_relations()
166+
{
167+
$postA = Post::create(['title' => 'foo']);
168+
$postB = Post::create(['title' => 'bar']);
169+
170+
foreach (range(1, 10) as $i) {
171+
$postA->comments()->create(['body' => 'ok']);
172+
$postB->comments()->create(['body' => 'ok']);
173+
}
174+
175+
$results = Search::new()
176+
->add(Post::with('comments'), 'title')
177+
->get('foo');
178+
179+
$this->assertCount(1, $results);
180+
$this->assertTrue($results->first()->relationLoaded('comments'));
181+
}
163182
}

0 commit comments

Comments
 (0)