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

Sort by #24

Closed
strebl opened this issue Apr 30, 2019 · 8 comments
Closed

Sort by #24

strebl opened this issue Apr 30, 2019 · 8 comments

Comments

@strebl
Copy link

strebl commented Apr 30, 2019

Hi @dillingham

Thanks for the package, really like it so far.

But I encountered a problem:
I need the items in a specific order. Is that possible?
It seems you force the order to be alphabetically.

@dillingham
Copy link
Owner

That is how Nova handles associatable resources also

GautierDele added a commit to GautierDele/nova-attach-many that referenced this issue Feb 12, 2021
Hello there,

To follow dillingham#24, sorting by display isn't necessary since the user already provided the sort in nova

Gautier
@rreynier
Copy link

Bummer this was closed, I was looking for something similar, but since this package doesn't offer any sort of configuration had to add a global scope like this:

protected static function boot()
{
    parent::boot();
    
    static::addGlobalScope('order', function (Builder $builder) {
        $builder->orderBy('sort_order', 'asc');
    });
}

Certainly not ideal, because I dislike using global scopes like this.

@dillingham
Copy link
Owner

@rreynier i think this was solved in #85

@YasamDevelopment
Copy link

I'm afraid I'm probably misunderstanding something (or many things!), but here is how I'm seeing things:

  • I'm using this field in Nova (of course!)
  • The items in this field are being listed in order of ID (not alphabetically as OP said).
  • I'm using latest version (1.3.0) [so Don't sort since nova did it before #85 is included in what I'm testing]
  • But I'm still seeing the list display in order of ID.
  • Don't sort since nova did it before #85 seems to be related to 'not sorting'.
  • I don't see any way that Nova provides that would allow me to sort the content of this list.
  • So I 'think', in order to sort these items, this package needs to have some kind of 'configuration' to allow the user to specify how to sort.

Please correct me if I'm wrong. Thanks.

@YasamDevelopment
Copy link

YasamDevelopment commented May 4, 2021

I just added a simple Pull Request (#89) to allow for sorting. I just noticed in currently only supports ASC sort order, but that can probably easily be changed.... I'd just like to get some comments from @dillingham & others to see if I'm on the right track.

@d13r
Copy link

d13r commented May 11, 2021

@YasamDevelopment After digging into the code I discovered it can be done by adding this method to the resource class:

    public static function relatableApplications(NovaRequest $request, $query)
    {
        return $query->orderBy('name');
    }

Where Applications is the plural of the target resource class name ($method = 'relatable'.Str::plural(class_basename($model))) - e.g. Application in this example:

    public function fields(Request $request)
    {
        return [
            // ...
            AttachMany::make('Select Applications', 'applications', Application::class),
        ];
    }

If that method doesn't exist, it falls back to relatableQuery(), so that can be used to set a default.

HTH

@briggsm
Copy link

briggsm commented May 12, 2021

@davejamesmiller , Thank you! Wow, how did I ever miss that documentation?! (I guess it didn't help that Nova documented it in their Authorization section - https://nova.laravel.com/docs/3.0/resources/authorization.html#relatable-filtering - which I hadn't paid much attention to since I was not concerned with Authorization within Nova.)

But anyhow, this is great information to know - thank you again for sharing with me (and others who might also come here searching). I implemented it in my code, and it works beautifully.

@d13r
Copy link

d13r commented May 12, 2021

Haha, I missed the documentation too - was looking for "sort" or "order by"!

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

6 participants