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 computed field #70

Open
katieben opened this issue Apr 4, 2019 · 3 comments
Open

Sort by computed field #70

katieben opened this issue Apr 4, 2019 · 3 comments
Labels
package-idea Suggestions for packages

Comments

@katieben
Copy link

katieben commented Apr 4, 2019

Package idea: I'd love to be able to sort by computed field. Right now I'm just adding the field as a regular field, and periodically double-checking it (ew :). Anyone have a smarter idea?

@mattstauffer mattstauffer added the package-idea Suggestions for packages label Apr 4, 2019
@mattstauffer mattstauffer changed the title Sort by computed field [please tag: package idea] Sort by computed field Apr 4, 2019
@mattstauffer
Copy link
Member

mattstauffer commented Apr 4, 2019

laravel/nova-issues#85

So, there are SOME things you can do, as you can see in that issue above. But one of the authors of Nova, David Hemphill, also said it's going to be tough to do it without breaking pagination--probably because it's using MySQL-based sort and you can't do that with a PHP-computed property.

With your periodically double-checking option, if you get stuck there and no one can solve this particular issue, one option (and maybe this is what you mean with periodical double-check it) is to set a trigger--either a cron job, or every time an event happens that would impact the computation of this field--that then makes Laravel calculate it and save it to the regular computed field. Like a projection.

Not sure where that lines up with your specific use case. Like I mentioned in the previous issue, I hope we can solve this because I've definitely run into it before!

@katieben
Copy link
Author

katieben commented Apr 4, 2019

Yep, that's what I mean. It would be nice to have the option to sort via Vue, for the models that's appropriate for. Then it would be doable! I don't have a good sense of how much data js can deal with, but I've done it by hand (not Nova) for projects with the amount of data in my use case, and it's a perfect solution.

@tpetry
Copy link

tpetry commented Feb 9, 2020

Why not extend nova resource sorting by the ability to define raw sql order by criteria? Like this:

Boolean::make('Package', fn () => $this->model()->getAttribute('types')['package'])
    ->sortableDb(fn () => "types->'package'"),

You would (as before) define a closure for php on how to render the information and define a database operation which will sort the rows the same like you did with your render function. This string could be simply be used with eloquents orderByRaw operation.

It's the only way it could work and it's a very sane approach.

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

No branches or pull requests

3 participants