laravel-query-builder
laravel-query-builder copied to clipboard
Not sorted by `defaultSort` if only invalid sorts in URL
Expected Behavior
If none of the sort parameters in the URL are actually taken into account, because none of them is allowed, then the default sort should be used.
Actual Behavior
If I specify an invalid sort in the URL, the default sort will not be used.
Steps To Reproduce
Environment
Version | |
---|---|
laravel/framework | 10.48.2 |
spatie/laravel-query-builder | 6.0.0 |
php | 8.2.17 |
Allow invalid sort parameters in query
'disable_invalid_sort_query_exception' => true,
Register sorting by name
->allowedSorts([
AllowedSort::field('name'),
])
->defaultSort('-name')
Open page without sort query /files
As you can see in the image and the SQL snippet below, the list is sorted by name in descending order, which is the expected default.
order by `name` desc
Open page and sort by name `/files?sort=name
As you can see in the image and the SQL snippet below, the list is sorted by name in ascending order, which is specified in the URL.
order by `name` asc
Open page and sort by invalid field /files?sort=password
As you can see in the image, the list is sorted by name (actually by ID) in ascending order, which is not expected, as the default should be descending order. There is no order by
specified in the sql query.