Skip to content

Commit

Permalink
Add Sorts docs
Browse files Browse the repository at this point in the history
  • Loading branch information
luilliarcec committed Oct 8, 2023
1 parent 33e9d14 commit 6265f38
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,47 @@ QueryBuilder::for(Book::class)
]);
```

### Sorts

#### RelationSort

To sort by fields of related tables you must use `join`, there is no easy way to do it from eloquent,
so you can use `TeamQ\QueryBuilder\Sorts\RelationSort`, this class receives the type of `join` as a parameter.

```php
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use TeamQ\QueryBuilder\Sorts\RelationSort;

QueryBuilder::for(Book::class)
->allowedSorts([
AllowedSort::custom('author.name', new RelationSort(JoinType::Inner)),
])
```

#### CaseSort

If you use enums or states, where each enum or state is represented by a number, you may want to sort by name
of that enum or state and not by the number, then you can use `TeamQ\QueryBuilder\Sorts\CaseSort`.

You must pass an array `[$key => $value]`, which will be used to generate the sort.

```php
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use TeamQ\QueryBuilder\Sorts\CaseSort;

QueryBuilder::for(Book::class)
->allowedSorts([
AllowedSort::custom('state', new CaseSort([
1 => 'Active',
2 => 'Rejected',
3 => 'Deleted',
4 => 'Completed',
])),
]);
```

## Testing

Can use docker-compose to run
Expand Down
4 changes: 4 additions & 0 deletions tests/Sorts/CaseSortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@
fn ($book) => $book->classification->toBe(BookClassificationEnum::Adults),
);
});

it('sort records by relationship fields', function () {

})->skip('WIP');

0 comments on commit 6265f38

Please sign in to comment.