Skip to content

Commit

Permalink
Merge pull request #79 from one2tek/2.x
Browse files Browse the repository at this point in the history
Merge 2.x to Master
  • Loading branch information
gentritabazi committed Sep 10, 2021
2 parents 58f63e4 + 072bf0c commit e7280f1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
- Advanced Usage
- [Quick start](advanced_usage.md?id=avanced_usage)
- [Custom Sort](advanced_usage.md?id=custom-sort)
- [Custom Filter](advanced_usage.md?id=custom-filter)
- [Custom Filter](advanced_usage.md?id=custom-filter)
- [Include Soft Deleted](advanced_usage.md?id=include-soft-deleted)
8 changes: 8 additions & 0 deletions docs/advanced_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public function filterName($queryBuilder, $method, $operator, $value, $clauseOpe
{
//
}
```

# Include Soft Deleted

By laravel soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to be included in a query's results by calling the `withTrashed` parameter on the url:

```console
{base_url}/users?withTrashed=1
```
20 changes: 18 additions & 2 deletions src/Controllers/LaravelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ protected function parseOrderByRandom($value)
return $value;
}

/**
* Parse with trashed.
*
* @param string $value
* @return bool
*/
protected function parseWithTrashed($value)
{
$value = $value ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : false;

return $value;
}

/**
* Parse filters.
*
Expand Down Expand Up @@ -342,7 +355,8 @@ protected function parseResourceOptions($request = null)
'append' => null,
'sortByDesc' => null,
'sortByAsc' => null,
'orderByRandom' => false
'orderByRandom' => false,
'withTrashed' => false
], $this->defaults);

$selects = $this->parseSelects($request->get('selects', $this->defaults['selects']));
Expand All @@ -365,6 +379,7 @@ protected function parseResourceOptions($request = null)
$sortByDesc = $this->parseSortByDesc($request->get('sortByDesc', $this->defaults['sortByDesc']));
$sortByAsc = $this->parseSortByAsc($request->get('sortByAsc', $this->defaults['sortByAsc']));
$orderByRandom = $this->parseOrderByRandom($request->get('orderByRandom', $this->defaults['orderByRandom']));
$withTrashed = $this->parseWithTrashed($request->get('withTrashed', $this->defaults['withTrashed']));

$data = [
'select' => $select,
Expand All @@ -386,7 +401,8 @@ protected function parseResourceOptions($request = null)
'append' => $append,
'sortByDesc' => $sortByDesc,
'sortByAsc' => $sortByAsc,
'orderByRandom' => $orderByRandom
'orderByRandom' => $orderByRandom,
'withTrashed' => $withTrashed
];

$this->validateResourceOptions($data);
Expand Down
9 changes: 9 additions & 0 deletions src/Database/EloquentBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected function applyResourceOptions(Builder $queryBuilder, array $options =
$this->applyOrderByRandom($queryBuilder, $orderByRandom);
}

if( (isset($withTrashed) && $withTrashed)) {
$this->applyWithTrashed($queryBuilder);
}

return $queryBuilder;
}

Expand Down Expand Up @@ -149,6 +153,11 @@ protected function applyOrderByRandom(Builder $queryBuilder, bool $orderByRaw)
$queryBuilder->orderByRaw('RAND()');
}

protected function applyWithTrashed(Builder $queryBuilder)
{
$queryBuilder->withTrashed();
}

protected function applyHas(Builder $queryBuilder, array $relations = [])
{
foreach ($relations as $relation) {
Expand Down

0 comments on commit e7280f1

Please sign in to comment.