Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting on ManyToMany and Index view is possible #60

Closed
Naoray opened this issue Nov 16, 2020 · 4 comments
Closed

Sorting on ManyToMany and Index view is possible #60

Naoray opened this issue Nov 16, 2020 · 4 comments

Comments

@Naoray
Copy link
Contributor

Naoray commented Nov 16, 2020

The documentation states that

NB! The resource can only be sorted on either the Index view or the HasMany list view, but not both!

That's not entirely true. If you name the column name on the PivotModel different than on your Model where you want to sort in the index view (e.g. PivotModel column name: sort_order, Model column name: order_column) and then add the following method to your Nova Resource

public static function getSortability(NovaRequest $request)
    {
        $response = HasSortableManyToManyRows::getSortability($request);

        if (!$request->viaManyToMany()) {
            return (object)[
                'model' => $response->model,
                'sortable' => $response->sortable,
                'sortOnBelongsTo' => false,
                'sortOnHasMany' => $response->sortOnHasMany,
            ];
        }

        return $response;
    }

Don't forget to change the order_column_name on your Model to order_column.

@Tarpsvo
Copy link
Collaborator

Tarpsvo commented Nov 18, 2020

Yeah, it's possible when using multiple order columns. But providing that out of the box and documenting it so that people actually understand it is quite a challenge. ;)

@Naoray
Copy link
Contributor Author

Naoray commented Nov 18, 2020

@Tarpsvo alright. I thought I just let you know in case you didn't. Maybe you can just link to this issue from the documentation somehow, so other ppl encountering this do have a clue on where to start with this.

@Tarpsvo Tarpsvo changed the title Sorting on HasMany and Index view is possible Sorting on ManyToMany and Index view is possible Dec 4, 2020
@Tarpsvo
Copy link
Collaborator

Tarpsvo commented Dec 4, 2020

Added a reference to this issue in the docs. :) Thanks!

@nammet
Copy link

nammet commented Aug 12, 2021

The getSortability method above will cause the global search to break. I've solved that by adding an additional check:

public static function getSortability($request)
    {
        $response = HasSortableManyToManyRows::getSortability($request);

        if (is_null($response)) {
            return (object)['canSort' => false];
        }

        if (!$request->viaManyToMany()) {
            return (object)[
                'model' => $response->model,
                'sortable' => $response->sortable,
                'sortOnBelongsTo' => false,
                'sortOnHasMany' => $response->sortOnHasMany,
            ];
        }

        return $response;
    }  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants