diff --git a/src/Contracts/RepositoryContract.php b/src/Contracts/RepositoryContract.php index 0ec65e4..307e1bf 100644 --- a/src/Contracts/RepositoryContract.php +++ b/src/Contracts/RepositoryContract.php @@ -5,7 +5,6 @@ use Illuminate\Support\Collection; use Illuminate\Pagination\Paginator; use Illuminate\Database\Eloquent\Model; -use Torann\LaravelRepository\Exceptions\RepositoryException; interface RepositoryContract { @@ -113,7 +112,7 @@ public function pluck($value, $key = null); * @param null $limit * @param array $columns * - * @return Paginator + * @return \Illuminate\Contracts\Pagination\Paginator */ public function paginate($limit = null, $columns = ['*']); @@ -123,7 +122,7 @@ public function paginate($limit = null, $columns = ['*']); * @param null $limit * @param array $columns * - * @return \Illuminate\Pagination\Paginator + * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($limit = null, $columns = ['*']); @@ -168,10 +167,11 @@ public function toSql(); * Add a message to the repository's error messages. * * @param string $message + * @param string $key * - * @return null + * @return self */ - public function addError($message); + public function addError($message, string $key = 'message'); /** * Get the repository's error messages. diff --git a/src/Repositories/AbstractRepository.php b/src/Repositories/AbstractRepository.php index b68519a..d190850 100755 --- a/src/Repositories/AbstractRepository.php +++ b/src/Repositories/AbstractRepository.php @@ -319,6 +319,29 @@ public function getOrderBy() return $this->orderBy; } + /** + * Set searchable array. + * + * @param array|string $key + * @param mixed $value + * + * @return self + */ + public function setSearchable($key, $value = null) + { + // Allow for a batch assignment + if (is_array($key) === false) { + $key = [$key => $value]; + } + + // Update the searchable values + foreach ($key as $k => $v) { + $this->searchable[$k] = $v; + } + + return $this; + } + /** * Return searchable keys. * @@ -381,7 +404,9 @@ public function search($queries) $value = Arr::get($queries, $param, ''); // Validate value - if ($value === '' || $value === null) continue; + if ($value === '' || $value === null) { + continue; + } // Columns should be an array $columns = (array) $columns; @@ -626,6 +651,21 @@ public function makeModel() return $this->modelInstance = new $this->model; } + /** + * Get a new query builder instance with the applied + * the order by and scopes. + * + * @param bool $skipOrdering + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function getBuilder(bool $skipOrdering = false) + { + $this->newQuery($skipOrdering); + + return $this->query; + } + /** * Get the raw SQL statements for the request * @@ -685,14 +725,15 @@ protected function applyScope() * Add a message to the repository's error messages. * * @param string $message + * @param string $key * - * @return null + * @return self */ - public function addError($message) + public function addError($message, string $key = 'message') { - $this->getErrors()->add('message', $message); + $this->getErrors()->add($key, $message); - return null; + return $this; } /** diff --git a/src/Traits/Cacheable.php b/src/Traits/Cacheable.php index e4fbdc6..7c70a54 100644 --- a/src/Traits/Cacheable.php +++ b/src/Traits/Cacheable.php @@ -74,7 +74,7 @@ public function skippedCache() * * @return string */ - public function getCacheKey($method, $args = null, $tag) + public function getCacheKey($method, $args = null, $tag = '') { // Sort through arguments foreach ($args as &$a) { @@ -86,7 +86,8 @@ public function getCacheKey($method, $args = null, $tag) // Create hash from arguments and query $args = serialize($args) . serialize($this->getScopeQuery()); - return sprintf('%s-%s@%s-%s', + return sprintf( + '%s-%s@%s-%s', config('app.locale'), $tag, $method,