statamic-rad-pack / runway

Eloquently manage your database models in Statamic.
https://statamic.com/runway
MIT License
107 stars 43 forks source link

Resource listing sort order is not applied correctly #437

Closed simonworkhouse closed 4 months ago

simonworkhouse commented 4 months ago

Description

Sort ordering in the admin panel isn't being applied unless an order has already been applied by the runwayListing scope.

In the method \StatamicRadPack\Runway\Http\Controllers\CP\ResourceListingController::index(...) the following check only successfully applies a user selected sort order if an order has already been applied to the query.

        $query->when($query->getQuery()->orders, function ($query) use ($request) {
            if ($request->input('sort')) {
                $query->reorder($request->input('sort'), $request->input('order'));
            }
        }, fn ($query) => $query->orderBy($resource->orderBy(), $resource->orderByDirection()));

One can see that $query->getQuery()->orders will be null unless $query->orderBy(...) has already been called.

Steps to reproduce

  1. Attempt to apply a non-default sort order in the admin panel for a model that doesn't call $query->orderBy(...) in it's runwayListing scope.

See https://github.com/statamic-rad-pack/runway/commit/c9ec2aec506a195782f18c4924f84ce990266ecb for an examlpe test that reproduces this issue.

Environment

Tests have been executed outside of an application environment.

ryanmitchell commented 4 months ago

Yeah some of the refactoring did away with that from the original code in the PR. I've opened https://github.com/statamic-rad-pack/runway/pull/438 to fix it.