Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Fix scope
Browse files Browse the repository at this point in the history
```
$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);
    }
```
  • Loading branch information
ruchengtang authored Mar 6, 2019
1 parent 8d0f219 commit 60a37b8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Repositories/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 60a37b8

Please sign in to comment.