Skip to content

Commit 033d4e7

Browse files
committed
Support for counting results
1 parent dc69d3a commit 033d4e7

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
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.7.0 - 2020-12-16
6+
7+
- Added a `count` method.
8+
59
## 1.6.0 - 2020-12-16
610

711
- Allow empty search terms without selecting columns

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ Search::add(Post::class)
172172
->get();
173173
```
174174

175+
### Counting records
176+
177+
You can count the number of results with the `count` method:
178+
179+
```php
180+
Search::add(Post::published(), 'title')
181+
->add(Video::where('views', '>', 2500), 'title')
182+
->count('compile');
183+
```
184+
175185
### Standalone parser
176186

177187
You can use the parser with the `parseTerms` method:

src/Searcher.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
7+
use Illuminate\Database\Query\Builder as QueryBuilder;
78
use Illuminate\Pagination\Paginator;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Facades\DB;
@@ -287,12 +288,12 @@ private function buildQueries(): Collection
287288
}
288289

289290
/**
290-
* Compiles all queries to one big one which binds everything together
291-
* using UNION statements.
292-
*
293-
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
294-
*/
295-
private function getIdAndOrderAttributes()
291+
* Compiles all queries to one big one which binds everything together
292+
* using UNION statements.
293+
*
294+
* @return
295+
*/
296+
private function getCompiledQueryBuilder(): QueryBuilder
296297
{
297298
$queries = $this->buildQueries();
298299

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

305306
// sort by the given columns and direction
306-
$firstQuery->orderBy(DB::raw($this->makeOrderBy()), $this->orderByDirection);
307+
return $firstQuery->orderBy(DB::raw($this->makeOrderBy()), $this->orderByDirection);
308+
}
309+
310+
/**
311+
* Paginates the compiled query or fetches all results.
312+
*
313+
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
314+
*/
315+
private function getIdAndOrderAttributes()
316+
{
317+
$query = $this->getCompiledQueryBuilder();
307318

308319
// get all results or limit the results by pagination
309320
return $this->perPage
310-
? $firstQuery->paginate($this->perPage, ['*'], $this->pageName, $this->page)
311-
: $firstQuery->get();
321+
? $query->paginate($this->perPage, ['*'], $this->pageName, $this->page)
322+
: $query->get();
312323

313324
// the collection will be something like:
314325
//
@@ -358,6 +369,19 @@ private function getModelsPerType($results)
358369
// ]
359370
}
360371

372+
/**
373+
* Retrieve the "count" result of the query.
374+
*
375+
* @param string $terms
376+
* @return integer
377+
*/
378+
public function count(string $terms = null): int
379+
{
380+
$this->initializeTerms($terms ?: '');
381+
382+
return $this->getCompiledQueryBuilder()->count();
383+
}
384+
361385
/**
362386
* Initialize the search terms, execute the search query and retrieve all
363387
* models per type. Map the results to a Eloquent collection and set

tests/SearchTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public function it_can_search_in_multiple_columns()
5454
$this->assertFalse($results->contains($postB));
5555
}
5656

57+
/** @test */
58+
public function it_can_count_the_results()
59+
{
60+
$postA = Post::create(['title' => 'foo']);
61+
$postB = Post::create(['title' => 'bar']);
62+
$videoA = Video::create(['title' => 'foo']);
63+
$videoB = Video::create(['title' => 'bar', 'subtitle' => 'foo']);
64+
65+
$count = Search::add(Post::class, 'title')
66+
->add(Video::class, ['title', 'subtitle'])
67+
->count('foo');
68+
69+
$this->assertEquals(3, $count);
70+
}
71+
5772
/** @test */
5873
public function it_can_search_for_a_phrase()
5974
{

0 commit comments

Comments
 (0)