Skip to content

Commit

Permalink
Support for counting results
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Dec 16, 2020
1 parent dc69d3a commit 033d4e7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 1.7.0 - 2020-12-16

- Added a `count` method.

## 1.6.0 - 2020-12-16

- Allow empty search terms without selecting columns
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ Search::add(Post::class)
->get();
```

### Counting records

You can count the number of results with the `count` method:

```php
Search::add(Post::published(), 'title')
->add(Video::where('views', '>', 2500), 'title')
->count('compile');
```

### Standalone parser

You can use the parser with the `parseTerms` method:
Expand Down
42 changes: 33 additions & 9 deletions src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -287,12 +288,12 @@ private function buildQueries(): Collection
}

/**
* Compiles all queries to one big one which binds everything together
* using UNION statements.
*
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
private function getIdAndOrderAttributes()
* Compiles all queries to one big one which binds everything together
* using UNION statements.
*
* @return
*/
private function getCompiledQueryBuilder(): QueryBuilder
{
$queries = $this->buildQueries();

Expand All @@ -303,12 +304,22 @@ private function getIdAndOrderAttributes()
$queries->each(fn (Builder $query) => $firstQuery->union($query));

// sort by the given columns and direction
$firstQuery->orderBy(DB::raw($this->makeOrderBy()), $this->orderByDirection);
return $firstQuery->orderBy(DB::raw($this->makeOrderBy()), $this->orderByDirection);
}

/**
* Paginates the compiled query or fetches all results.
*
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
private function getIdAndOrderAttributes()
{
$query = $this->getCompiledQueryBuilder();

// get all results or limit the results by pagination
return $this->perPage
? $firstQuery->paginate($this->perPage, ['*'], $this->pageName, $this->page)
: $firstQuery->get();
? $query->paginate($this->perPage, ['*'], $this->pageName, $this->page)
: $query->get();

// the collection will be something like:
//
Expand Down Expand Up @@ -358,6 +369,19 @@ private function getModelsPerType($results)
// ]
}

/**
* Retrieve the "count" result of the query.
*
* @param string $terms
* @return integer
*/
public function count(string $terms = null): int
{
$this->initializeTerms($terms ?: '');

return $this->getCompiledQueryBuilder()->count();
}

/**
* Initialize the search terms, execute the search query and retrieve all
* models per type. Map the results to a Eloquent collection and set
Expand Down
15 changes: 15 additions & 0 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ public function it_can_search_in_multiple_columns()
$this->assertFalse($results->contains($postB));
}

/** @test */
public function it_can_count_the_results()
{
$postA = Post::create(['title' => 'foo']);
$postB = Post::create(['title' => 'bar']);
$videoA = Video::create(['title' => 'foo']);
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);

$count = Search::add(Post::class, 'title')
->add(Video::class, ['title', 'subtitle'])
->count('foo');

$this->assertEquals(3, $count);
}

/** @test */
public function it_can_search_for_a_phrase()
{
Expand Down

0 comments on commit 033d4e7

Please sign in to comment.