From 60a37b87afe6fa36dc9f26106276f68bdd354862 Mon Sep 17 00:00:00 2001 From: Rucheng Tang Date: Wed, 6 Mar 2019 11:07:47 +0800 Subject: [PATCH] Fix scope ``` $this->ofInput('name', $searchData, 'and', true) ->ofInput('cellphone', $searchData, 'and', true) ->findAll(); ``` Custom base model method: ``` /** * Search from request input. * * @param Builder $query * @param string $field * @param string|array $value * @param string $boolean * @param bool $like * * @return Builder */ public function scopeOfInput($query, $field, $value, $boolean = 'and', $like = false) { if (is_array($value) && !isset($value[$field])) { return $query; } return $this->_where($query, $field, $value[$field], $boolean, $like); } ``` --- src/Repositories/BaseRepository.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Repositories/BaseRepository.php b/src/Repositories/BaseRepository.php index b0df753..16da431 100644 --- a/src/Repositories/BaseRepository.php +++ b/src/Repositories/BaseRepository.php @@ -216,8 +216,10 @@ protected function prepareQuery($model) } // Add a "scope" to the query - foreach ($this->scopes as $scope => $parameters) { - $model = $model->{$scope}(...$parameters); + foreach ($this->scopes as $name => $scopes) { + foreach ($scopes as $parameters) { + $model = $model->{$name}(...$parameters); + } } // Set the "offset" value of the query @@ -396,7 +398,7 @@ public function whereHas($relation, Closure $callback = null, $operator = '>=', */ public function scope($name, array $parameters = []) { - $this->scopes[$name] = $parameters; + $this->scopes[$name][] = $parameters; return $this; }