spatie / laravel-query-builder

Easily build Eloquent queries from API requests
https://spatie.be/docs/laravel-query-builder
MIT License
4k stars 392 forks source link

Not sorted by `defaultSort` if only invalid sorts in URL #942

Open ManuelLeiner opened 3 months ago

ManuelLeiner commented 3 months ago

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.

image
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.

image
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.

image