Skip to content

Commit

Permalink
Eager load
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Jul 7, 2020
1 parent 689e574 commit 3dbb04c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ $results = Search::new()
->get('foo');
```

### Add wildcard on left side of the term

```php
$results = Search::new()
->add(Post::class, 'title')
->add(Video::class, 'title')
->wildcardLeft()
->get('foo');
```

### Eager load relations

```php
$results = Search::new()
->add(Post::with('comments'), 'title')
->add(Video::with('likes'), 'title')
->get('foo');
```

### Testing

``` bash
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
"illuminate/support": "^6.0|^7.0"
},
"require-dev": {
"mockery/mockery": "^1.3",
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0"
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/PendingQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getFreshBuilder(): Builder

public function newQueryWithoutScopes(): Builder
{
return $this->getModel()->newQueryWithoutScopes();
return $this->getFreshBuilder();
}

public function getQualifiedColumns(): Collection
Expand Down
19 changes: 19 additions & 0 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,23 @@ public function it_can_paginate_the_results()
$this->assertTrue($resultsPage2->first()->is($postA));
$this->assertTrue($resultsPage2->last()->is($videoA));
}

/** @test */
public function it_can_eager_load_relations()
{
$postA = Post::create(['title' => 'foo']);
$postB = Post::create(['title' => 'bar']);

foreach (range(1, 10) as $i) {
$postA->comments()->create(['body' => 'ok']);
$postB->comments()->create(['body' => 'ok']);
}

$results = Search::new()
->add(Post::with('comments'), 'title')
->get('foo');

$this->assertCount(1, $results);
$this->assertTrue($results->first()->relationLoaded('comments'));
}
}

0 comments on commit 3dbb04c

Please sign in to comment.