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

[Feature request] Sort relation fields by column (BelongsTo, HasMany etc.) #1241

Closed
StoyanPenev opened this issue Jan 17, 2019 · 6 comments
Closed
Labels
implemented Feature Request implemented request Feature Request

Comments

@StoyanPenev
Copy link

StoyanPenev commented Jan 17, 2019

Hi there!

Currently there is no control over how the related fields are sorted when displayed in the select dropdown lists.
My proposal is to expose a sortBy method on all relation fields and then also update the AssociatableController::index() that prepares the resources listing.
Like so:

BelongsTo::make('Product', null, Product::class)
            ->sortBy('updated_at', 'asc')

So in the AssociatableController::index() method instead of getting resources like

'resources' => $field->buildAssociatableQuery($request, $withTrashed)->get()
                        ->mapInto($field->resourceClass)
                        ->filter->authorizedToAdd($request, $request->model())
                        ->map(function ($resource) use ($request, $field) {
                            return $field->formatAssociatableResource($request, $resource);
                        })->sortBy('display')->values(),

See we have a hardcoded ->sortBy('display')->values() as default ordering, what if we want to display latest updated records first?

A better approach is to perform the ordering based on column and direction (taken from above)
so AssociatableController::index() should perform ordering like:

'resources' => $field->buildAssociatableQuery($request, $withTrashed)->get()
                        ->mapInto($field->resourceClass)
                        ->filter->authorizedToAdd($request, $request->model())
                        ->{$field->sortByMethod}($field->sortByColumn)
                        ->map(function ($resource) use ($request, $field) {
                            return $field->formatAssociatableResource($request, $resource);
                        })->values(),

So all changes that need to be done are in the controller index method and probably a shared Sortable trait for the relation fields.

public function sortBy(string $column, string $sortBy = 'desc')
    {
        $this->sortByColumn = $column;
        $this->sortByMethod = $sortBy != 'desc' ? 'sortBy' : 'sortByDesc';

        return $this;
    }

finally we can have a default sortByColumn taken from the related $resource::title
so all relation fields should have or this can be defaulted in the controller (not very clear though)

public function __construct($name, $attribute = null, $resource = null)
    {
        parent::__construct($name, $attribute);

        $resource = $resource ?? ResourceRelationshipGuesser::guessResource($name);

        $this->resourceClass = $resource;
        $this->sortByColumn = $resource::$title;
        $this->resourceName = $resource::uriKey();
        $this->belongsToRelationship = $this->attribute;
    }

I think this can be a great addition to the otherwise awesome Laravel Nova.

It will also resolve one of the closed issues
#156 (comment)

Greetings,
Stoyan

@StoyanPenev StoyanPenev changed the title Sort relation fields by column (BelongsTo, HasMany etc.) [Feature request] Sort relation fields by column (BelongsTo, HasMany etc.) Jan 17, 2019
@bonzai bonzai added the request Feature Request label Jan 17, 2019
@bonzai bonzai closed this as completed Jan 17, 2019
@didix16
Copy link

didix16 commented Feb 19, 2020

Hello, have this functionality been implemented yet?

Thanks

@larshanskrause
Copy link

@didix16 As far as I can tell this hasn't been implemented as of version 3.0.2. I'd really like this feature, too.

@didix16
Copy link

didix16 commented Mar 17, 2020

Oh, ok. Thanks @larshanskrause . We will keep waiting then.

@bhavinzwt
Copy link

Is there any update on this from Nova?

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2021
@crynobone
Copy link
Member

@crynobone crynobone added the implemented Feature Request implemented label Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
implemented Feature Request implemented request Feature Request
Projects
None yet
Development

No branches or pull requests

6 participants