Skip to content

Commit

Permalink
Merge pull request #19 from one2tek/1.0
Browse files Browse the repository at this point in the history
1.0
  • Loading branch information
gent-fella-health authored Oct 16, 2020
2 parents 0070a28 + b267106 commit 9f54f64
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ Nested **has** statements may also be constructed using **"dot"** notation. For

`{base_url}/posts?has[]=comments.votes`

### Querying Relationship Absence

When accessing the records for a model, you may wish to limit your results based on the absence of a relationship. For example, imagine you want to retrieve all blog posts that don't have any comments. To do so, you may pass the name of the relationship to the **doesntHave**:

`{base_url}/posts?doesntHave[]=comments`

### Advanced Eager loading

Expand Down
3 changes: 3 additions & 0 deletions src/Controllers/LaravelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ protected function parseResourceOptions($request = null)
'withCount' => [],
'withs' => [],
'has' => [],
'doesntHave' => [],
'exludeGlobalScopes' => [],
'scope' => [],
'sort' => [],
Expand All @@ -251,6 +252,7 @@ protected function parseResourceOptions($request = null)
$withCount = $this->parseWithCount($request->get('withCount', $this->defaults['withCount']));
$withs = $request->get('with', $this->defaults['withs']);
$has = $request->get('has', $this->defaults['has']);
$doesntHave = $request->get('doesntHave', $this->defaults['doesntHave']);
$exludeGlobalScopes = $this->parseExludeGlobalScopes($request->get('exludeGlobalScopes', $this->defaults['exludeGlobalScopes']));
$scope = $request->get('scope', $this->defaults['scope']);
$sort = $this->parseSort($request->get('sort', $this->defaults['sort']));
Expand All @@ -266,6 +268,7 @@ protected function parseResourceOptions($request = null)
'withCount' => $withCount,
'withs' => $withs,
'has' => $has,
'doesntHave' => $doesntHave,
'exludeGlobalScopes' => $exludeGlobalScopes,
'modes' => $includes['modes'],
'scope' => $scope,
Expand Down
6 changes: 6 additions & 0 deletions src/Database/EloquentBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ protected function applyResourceOptions(Builder $queryBuilder, array $options =
$queryBuilder->has($relation);
}
}

if (isset($doesntHave)) {
foreach ($doesntHave as $relation) {
$queryBuilder->doesntHave($relation);
}
}

if (isset($exludeGlobalScopes)) {
if (!is_array($exludeGlobalScopes)) {
Expand Down

0 comments on commit 9f54f64

Please sign in to comment.