Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit c3deee1

Browse files
committed
New function
- add sum() function - add count() and sum() function to cache trait
1 parent cec6836 commit c3deee1

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
2+
## v.4.1.1
3+
- add functions
4+
* sum()
5+
- add functions to work with cache trait
6+
* count()
7+
* sum()
28
## v.4.1.0
39
- compatibility with PHP version 8.x (updated composer.json file)
410
- DateCriteria now accept "column" parameter

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ class ExampleController extends Controller {
131131
* orWhereHasMorph($relation, $types, $callback, $operator, $count)
132132
* whereDoesntHaveMorph($relation, $types, $callback)
133133
* orWhereDoesntHaveMorph($relation, $types, $callback)
134+
* sum($column)
134135

135136
##### Additional methods (Laravel only)
136137
* datatable() - return EloquentDataTable instance for records. In order to user with method,
137138
you must install suggested "yajra/laravel-datatables-oracle" package, and add "WithDatatable"
138139
trait in your repository of choice.
139140
#### Built-in criteria
140141
List of criteria, provider by default with this package:
141-
* DateCriteria - search records with given date range (by created_at field)
142+
* DateCriteria - search records with given date range
142143
* FindWhereCriteria
143144
* FindWhereInCriteria
144145
* FindWhereNotInCriteria
@@ -167,6 +168,8 @@ of choice. Trait will handle cache for methods:
167168
* findWhereNotIn()
168169
* paginate()
169170
* simplePaginate()
171+
* count()
172+
* sum()
170173

171174
You can user criteria with this functions, and results will be cached.
172175

src/Repositories/CoreRepository.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,26 @@ public function count(array $columns = ['*']): int
667667
return $results;
668668
}
669669

670+
/**
671+
* Retrieve the sum of the values of a given column.
672+
*
673+
* @param string $column
674+
*
675+
* @return mixed
676+
*
677+
* @author Wiktor Pacer
678+
*/
679+
public function sum(string $column)
680+
{
681+
$this->applyCriteria();
682+
683+
$results = $this->getEntity()->sum($column);
684+
685+
$this->makeEntity();
686+
687+
return $results;
688+
}
689+
670690
/**
671691
* Paginate results.
672692
*

src/Repositories/Interfaces/CoreRepositoryInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ public function chunk(int $limit, callable $callback, array $columns = ['*']): b
424424
*/
425425
public function count(array $columns = ['*']): int;
426426

427+
/**
428+
* Retrieve the sum of the values of a given column.
429+
*
430+
* @param string $column
431+
*
432+
* @return mixed
433+
*
434+
* @author Wiktor Pacer
435+
*/
436+
public function sum(string $column);
437+
427438
/**
428439
* Paginate results.
429440
*

src/Repositories/Traits/WithCache.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace PacerIT\LaravelRepository\Repositories\Traits;
44

5+
use Illuminate\Contracts\Container\BindingResolutionException;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Cache;
9+
use PacerIT\LaravelRepository\Repositories\Exceptions\RepositoryEntityException;
810
use PacerIT\LaravelRepository\Repositories\Interfaces\CoreRepositoryInterface;
911
use ReflectionObject;
1012

@@ -534,6 +536,65 @@ function () use ($perPage, $columns, $pageName, $page) {
534536
);
535537
}
536538

539+
/**
540+
* Count results.
541+
*
542+
* @param array $columns
543+
*
544+
* @throws BindingResolutionException
545+
* @throws RepositoryEntityException
546+
*
547+
* @return int
548+
*
549+
* @author Wiktor Pacer <[email protected]>
550+
*
551+
* @since 19/11/2019
552+
*/
553+
public function count(array $columns = ['*']): int
554+
{
555+
if ($this->skipCache || !$this->cacheActive()) {
556+
return parent::count($columns);
557+
}
558+
559+
$cacheKey = $this->getCacheKey(__FUNCTION__, func_get_args());
560+
561+
// Store or get from cache.
562+
return Cache::tags([$this->getTag()])->remember(
563+
$cacheKey,
564+
$this->getCacheTime(),
565+
function () use ($columns) {
566+
return parent::count($columns);
567+
}
568+
);
569+
}
570+
571+
/**
572+
* Retrieve the sum of the values of a given column.
573+
*
574+
* @param string $column
575+
*
576+
* @return int
577+
*
578+
* @author Wiktor Pacer
579+
*/
580+
public function sum(string $column)
581+
{
582+
if ($this->skipCache || !$this->cacheActive()) {
583+
return parent::sum($column);
584+
}
585+
586+
$cacheKey = $this->getCacheKey(__FUNCTION__, func_get_args());
587+
588+
// Store or get from cache.
589+
return Cache::tags([$this->getTag()])->remember(
590+
$cacheKey,
591+
$this->getCacheTime(),
592+
function () use ($column) {
593+
return parent::sum($column);
594+
}
595+
);
596+
}
597+
537598
/**
538599
* Try to get actual authenticated user ID.
539600
*

0 commit comments

Comments
 (0)